1 | /*
|
---|
2 | * TECS Generator
|
---|
3 | * Generator for TOPPERS Embedded Component System
|
---|
4 | *
|
---|
5 | * Copyright (C) 2008-2013 by TOPPERS Project
|
---|
6 | *--
|
---|
7 | * ä¸è¨èä½æ¨©è
|
---|
8 | ã¯ï¼ä»¥ä¸ã®(1)(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
9 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
10 | * å¤ã»åé
|
---|
11 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
12 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
13 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
14 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
15 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
16 | * ç¨ã§ããå½¢ã§åé
|
---|
17 | å¸ããå ´åã«ã¯ï¼åé
|
---|
18 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
19 | * è
|
---|
20 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
21 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
22 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
23 | * ç¨ã§ããªãå½¢ã§åé
|
---|
24 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
25 | * ã¨ï¼
|
---|
26 | * (a) åé
|
---|
27 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
28 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
29 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
30 | * (b) åé
|
---|
31 | å¸ã®å½¢æ
|
---|
32 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
33 | * å ±åãããã¨ï¼
|
---|
34 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
35 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
36 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
37 | 責ãããã¨ï¼
|
---|
38 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
39 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
40 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
41 | * å
|
---|
42 | 責ãããã¨ï¼
|
---|
43 | *
|
---|
44 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
45 | ã
|
---|
46 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
47 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
48 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
49 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
50 | *
|
---|
51 | * $Id: tSocketServer.c 270 2017-02-09 04:03:47Z coas-nagasima $
|
---|
52 | */
|
---|
53 |
|
---|
54 | /* #[<PREAMBLE>]#
|
---|
55 | * #[<...>]# ãã #[</...>]# ã§å²ã¾ããã³ã¡ã³ãã¯ç·¨éããªãã§ãã ãã
|
---|
56 | * tecsmerge ã«ãããã¼ã¸ã«ä½¿ç¨ããã¾ã
|
---|
57 | *
|
---|
58 | * å±æ§ã¢ã¯ã»ã¹ãã¯ã #_CAAM_#
|
---|
59 | * portNo int16_t ATTR_portNo
|
---|
60 | * sd int_t VAR_sd
|
---|
61 | *
|
---|
62 | * #[</PREAMBLE>]# */
|
---|
63 |
|
---|
64 | #include "tSocketServer_tecsgen.h"
|
---|
65 |
|
---|
66 | #include <errno.h>
|
---|
67 | #include <fcntl.h>
|
---|
68 | #include <sys/socket.h>
|
---|
69 | #include <sys/types.h>
|
---|
70 | #include <arpa/inet.h>
|
---|
71 | #include <unistd.h>
|
---|
72 | #include "errno2ER.h"
|
---|
73 |
|
---|
74 | #ifndef E_OK
|
---|
75 | #define E_OK 0 /* æ£å¸¸çµäº */
|
---|
76 | #define E_ID (-18) /* ä¸æ£IDçªå· */
|
---|
77 | #endif
|
---|
78 |
|
---|
79 |
|
---|
80 | /* åãå£é¢æ° #_TEPF_# */
|
---|
81 | /* #[<ENTRY_PORT>]# eC1
|
---|
82 | * entry port: eC1
|
---|
83 | * signature: sChannel
|
---|
84 | * context: task
|
---|
85 | * #[</ENTRY_PORT>]# */
|
---|
86 |
|
---|
87 | /* #[<ENTRY_FUNC>]# eC1_send
|
---|
88 | * name: eC1_send
|
---|
89 | * global_name: tSocketServer_eC1_send
|
---|
90 | * oneway: false
|
---|
91 | * #[</ENTRY_FUNC>]# */
|
---|
92 | ER
|
---|
93 | eC1_send(CELLIDX idx, const int8_t* buf, int16_t size, TMO tmo)
|
---|
94 | {
|
---|
95 | ER ercd = E_OK;
|
---|
96 | CELLCB *p_cellcb;
|
---|
97 | int sz;
|
---|
98 |
|
---|
99 | if (VALID_IDX(idx)) {
|
---|
100 | p_cellcb = GET_CELLCB(idx);
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | return(E_ID);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã */
|
---|
107 | #ifdef DEBUG
|
---|
108 | syslog( LOG_INFO, "tSocketServer: send" );
|
---|
109 | {
|
---|
110 | int i;
|
---|
111 | for( i = 0; i < size; i++ ){
|
---|
112 | printf( "%02X ", (unsigned char)buf[i] );
|
---|
113 | if( i % 8 == 7 )
|
---|
114 | putchar( '\n' );
|
---|
115 | }
|
---|
116 | putchar( '\n' );
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 | while(size>0){
|
---|
120 | if((sz=write( VAR_sd, buf, (size_t)size )) < 0 ){
|
---|
121 | ercd = errno2ER( errno );
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | else{
|
---|
125 | size -= sz;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | return(ercd);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* #[<ENTRY_FUNC>]# eC1_flush
|
---|
133 | * name: eC1_flush
|
---|
134 | * global_name: tSocketServer_eC1_flush
|
---|
135 | * oneway: false
|
---|
136 | * #[</ENTRY_FUNC>]# */
|
---|
137 | ER
|
---|
138 | eC1_flush(CELLIDX idx, TMO tmo)
|
---|
139 | {
|
---|
140 | ER ercd = E_OK;
|
---|
141 | CELLCB *p_cellcb;
|
---|
142 | if (VALID_IDX(idx)) {
|
---|
143 | p_cellcb = GET_CELLCB(idx);
|
---|
144 | }
|
---|
145 | else {
|
---|
146 | return(E_ID);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã */
|
---|
150 |
|
---|
151 | return(ercd);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* #[<ENTRY_FUNC>]# eC1_receive
|
---|
155 | * name: eC1_receive
|
---|
156 | * global_name: tSocketServer_eC1_receive
|
---|
157 | * oneway: false
|
---|
158 | * #[</ENTRY_FUNC>]# */
|
---|
159 | ER_UINT
|
---|
160 | eC1_receive(CELLIDX idx, int8_t* buf, int16_t size, TMO tmo)
|
---|
161 | {
|
---|
162 | ER ercd = E_OK;
|
---|
163 | ER_UINT sz;
|
---|
164 | CELLCB *p_cellcb;
|
---|
165 |
|
---|
166 | if (VALID_IDX(idx)) {
|
---|
167 | p_cellcb = GET_CELLCB(idx);
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | return(E_ID);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã */
|
---|
174 | if( (sz=read( VAR_sd, buf, (size_t)size )) < 0 )
|
---|
175 | ercd = errno2ER( errno );
|
---|
176 | else
|
---|
177 | ercd = sz;
|
---|
178 |
|
---|
179 | return(ercd);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* #[<ENTRY_FUNC>]# eC1_end_receive
|
---|
183 | * name: eC1_end_receive
|
---|
184 | * global_name: tSocketServer_eC1_end_receive
|
---|
185 | * oneway: false
|
---|
186 | * #[</ENTRY_FUNC>]# */
|
---|
187 | ER
|
---|
188 | eC1_end_receive(CELLIDX idx)
|
---|
189 | {
|
---|
190 | ER ercd = E_OK;
|
---|
191 | CELLCB *p_cellcb;
|
---|
192 | if (VALID_IDX(idx)) {
|
---|
193 | p_cellcb = GET_CELLCB(idx);
|
---|
194 | }
|
---|
195 | else {
|
---|
196 | return(E_ID);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã */
|
---|
200 |
|
---|
201 | return(ercd);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* #[<ENTRY_FUNC>]# eC1_reset
|
---|
205 | * name: eC1_reset
|
---|
206 | * global_name: tSocketServer_eC1_reset
|
---|
207 | * oneway: false
|
---|
208 | * #[</ENTRY_FUNC>]# */
|
---|
209 | ER
|
---|
210 | eC1_reset(CELLIDX idx)
|
---|
211 | {
|
---|
212 | ER ercd = E_OK;
|
---|
213 | CELLCB *p_cellcb;
|
---|
214 | if (VALID_IDX(idx)) {
|
---|
215 | p_cellcb = GET_CELLCB(idx);
|
---|
216 | }
|
---|
217 | else {
|
---|
218 | return(E_ID);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã */
|
---|
222 | syslog( LOG_INFO, "tSocketServer: reset" );
|
---|
223 |
|
---|
224 | return(ercd);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* #[<ENTRY_PORT>]# eOpener
|
---|
228 | * entry port: eOpener
|
---|
229 | * signature: sServerChannelOpener
|
---|
230 | * context: task
|
---|
231 | * #[</ENTRY_PORT>]# */
|
---|
232 |
|
---|
233 | /* #[<ENTRY_FUNC>]# eOpener_open
|
---|
234 | * name: eOpener_open
|
---|
235 | * global_name: tSocketServer_eOpener_open
|
---|
236 | * oneway: false
|
---|
237 | * #[</ENTRY_FUNC>]# */
|
---|
238 | ER
|
---|
239 | eOpener_open(CELLIDX idx)
|
---|
240 | {
|
---|
241 | ER ercd = E_OK;
|
---|
242 | CELLCB *p_cellcb;
|
---|
243 | int ssoc, soc;
|
---|
244 | struct sockaddr_in addr={ AF_INET };
|
---|
245 |
|
---|
246 | if (VALID_IDX(idx)) {
|
---|
247 | p_cellcb = GET_CELLCB(idx);
|
---|
248 | }
|
---|
249 | else {
|
---|
250 | return(E_ID);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã #_TEFB_# */
|
---|
254 | syslog( LOG_INFO, "tSocketServer: open" );
|
---|
255 |
|
---|
256 | ssoc=socket(AF_INET,SOCK_STREAM,0);
|
---|
257 | if(ssoc<0){
|
---|
258 | ercd = errno2ER( errno );
|
---|
259 | syslog( LOG_INFO, "tSocketServer: socket failed" );
|
---|
260 | return ercd;
|
---|
261 | }
|
---|
262 |
|
---|
263 | addr.sin_port = htons(ATTR_portNo);
|
---|
264 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
265 | if(bind(ssoc,(struct sockaddr*)&addr,sizeof(addr))<0){
|
---|
266 | ercd = errno2ER( errno );
|
---|
267 | syslog( LOG_INFO, "tSocketServer: bind failed" );
|
---|
268 | return ercd;
|
---|
269 | }
|
---|
270 |
|
---|
271 | if(listen(ssoc, 1 )<0){
|
---|
272 | ercd = errno2ER( errno );
|
---|
273 | syslog( LOG_INFO, "tSocketServer: listen failed" );
|
---|
274 | return ercd;
|
---|
275 | }
|
---|
276 |
|
---|
277 | soc=accept(ssoc, NULL, NULL);
|
---|
278 | if(soc<0){
|
---|
279 | ercd = errno2ER( errno );
|
---|
280 | syslog( LOG_INFO, "tSocketServer: accept failed" );
|
---|
281 | return ercd;
|
---|
282 | }
|
---|
283 | syslog( LOG_INFO, "tSocketServer: accepted" );
|
---|
284 |
|
---|
285 | close( ssoc );
|
---|
286 | VAR_sd = soc;
|
---|
287 |
|
---|
288 | return(ercd);
|
---|
289 | }
|
---|
290 |
|
---|
291 | /* #[<ENTRY_FUNC>]# eOpener_close
|
---|
292 | * name: eOpener_close
|
---|
293 | * global_name: tSocketServer_eOpener_close
|
---|
294 | * oneway: false
|
---|
295 | * #[</ENTRY_FUNC>]# */
|
---|
296 | ER
|
---|
297 | eOpener_close(CELLIDX idx)
|
---|
298 | {
|
---|
299 | ER ercd = E_OK;
|
---|
300 | CELLCB *p_cellcb;
|
---|
301 | if (VALID_IDX(idx)) {
|
---|
302 | p_cellcb = GET_CELLCB(idx);
|
---|
303 | }
|
---|
304 | else {
|
---|
305 | return(E_ID);
|
---|
306 | }
|
---|
307 |
|
---|
308 | /* ããã«å¦çæ¬ä½ãè¨è¿°ãã¾ã #_TEFB_# */
|
---|
309 | syslog( LOG_INFO, "tSocketServer: close" );
|
---|
310 | if( close( VAR_sd ) < 0 )
|
---|
311 | ercd = errno2ER( errno );
|
---|
312 |
|
---|
313 | return(ercd);
|
---|
314 | }
|
---|
315 |
|
---|
316 | /* #[<POSTAMBLE>]#
|
---|
317 | * ããããä¸ã«éåãå£é¢æ°ãæ¸ãã¾ã
|
---|
318 | * #[</POSTAMBLE>]#*/
|
---|