[388] | 1 | /*
|
---|
| 2 | * TECS Generator
|
---|
| 3 | * Generator for TOPPERS Embedded Component System
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2008-2013 by TOPPERS Project
|
---|
| 6 | *--
|
---|
| 7 | * 上記著作権者は,以下の(1)(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 8 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 9 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 10 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 11 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 12 | * スコード中に含まれていること.
|
---|
| 13 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 14 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 15 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 16 | * の無保証規定を掲載すること.
|
---|
| 17 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 18 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 19 | * と.
|
---|
| 20 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 21 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 22 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 23 | * 報告すること.
|
---|
| 24 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 25 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 26 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 27 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 28 | * 免責すること.
|
---|
| 29 | *
|
---|
| 30 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 31 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 32 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 33 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 34 | * の責任を負わない.
|
---|
| 35 | *
|
---|
| 36 | * $Id$
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | /* #[<PREAMBLE>]#
|
---|
| 40 | * Don't edit the comments between #[<...>]# and #[</...>]#
|
---|
| 41 | * These comment are used by tecsmerege when merging.
|
---|
| 42 | *
|
---|
| 43 | * attr access macro #_CAAM_#
|
---|
| 44 | * portNo int16_t ATTR_portNo
|
---|
| 45 | * serverAddr char_t* ATTR_serverAddr
|
---|
| 46 | * sd int_t VAR_sd
|
---|
| 47 | *
|
---|
| 48 | * #[</PREAMBLE>]# */
|
---|
| 49 |
|
---|
| 50 | #include "tSocketClient_tecsgen.h"
|
---|
| 51 |
|
---|
| 52 | #include <errno.h>
|
---|
| 53 | #include <fcntl.h>
|
---|
| 54 | #include <sys/socket.h>
|
---|
| 55 | #include <sys/types.h>
|
---|
| 56 | #include <arpa/inet.h>
|
---|
| 57 | #include <unistd.h>
|
---|
| 58 | #include "errno2ER.h"
|
---|
| 59 |
|
---|
| 60 | #ifndef E_OK
|
---|
| 61 | #define E_OK 0 /* 正常終了 */
|
---|
| 62 | #define E_ID (-18) /* 不正ID番号 */
|
---|
| 63 | #endif
|
---|
| 64 |
|
---|
| 65 | /* 受け口関数 #_TEPF_# */
|
---|
| 66 | /* #[<ENTRY_PORT>]# eC0
|
---|
| 67 | * entry port: eC0
|
---|
| 68 | * signature: sChannel
|
---|
| 69 | * context: task
|
---|
| 70 | * #[</ENTRY_PORT>]# */
|
---|
| 71 |
|
---|
| 72 | /* #[<ENTRY_FUNC>]# eC0_send
|
---|
| 73 | * name: eC0_send
|
---|
| 74 | * global_name: tSocketClient_eC0_send
|
---|
| 75 | * oneway: false
|
---|
| 76 | * #[</ENTRY_FUNC>]# */
|
---|
| 77 | ER
|
---|
| 78 | eC0_send(CELLIDX idx, const int8_t* buf, int16_t size, TMO tmo)
|
---|
| 79 | {
|
---|
| 80 | ER ercd = E_OK;
|
---|
| 81 | CELLCB *p_cellcb;
|
---|
| 82 | ssize_t sz;
|
---|
| 83 |
|
---|
| 84 | if (VALID_IDX(idx)) {
|
---|
| 85 | p_cellcb = GET_CELLCB(idx);
|
---|
| 86 | }
|
---|
| 87 | else {
|
---|
| 88 | return(E_ID);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /* ここに処理本体を記述します */
|
---|
| 92 | // mikan BUG buf の位置を更新していない
|
---|
| 93 | while(size>0){
|
---|
| 94 | if((sz=write( VAR_sd, buf, (ssize_t)size )) < 0 ){
|
---|
| 95 | #ifdef RPC_DEBUG
|
---|
| 96 | perror( "eC0_send" );
|
---|
| 97 | syslog( LOG_INFO, "eC0_send: sd %d, writing %d bytes, ercd=%d", VAR_sd, size, ercd );
|
---|
| 98 | #endif
|
---|
| 99 | ercd = errno2ER( errno );
|
---|
| 100 | break;
|
---|
| 101 | }
|
---|
| 102 | else{
|
---|
| 103 | size -= sz;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | return(ercd);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /* #[<ENTRY_FUNC>]# eC0_flush
|
---|
| 111 | * name: eC0_flush
|
---|
| 112 | * global_name: tSocketClient_eC0_flush
|
---|
| 113 | * oneway: false
|
---|
| 114 | * #[</ENTRY_FUNC>]# */
|
---|
| 115 | ER
|
---|
| 116 | eC0_flush(CELLIDX idx, TMO tmo)
|
---|
| 117 | {
|
---|
| 118 | ER ercd = E_OK;
|
---|
| 119 | CELLCB *p_cellcb;
|
---|
| 120 | if (VALID_IDX(idx)) {
|
---|
| 121 | p_cellcb = GET_CELLCB(idx);
|
---|
| 122 | }
|
---|
| 123 | else {
|
---|
| 124 | return(E_ID);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /* ここに処理本体を記述します */
|
---|
| 128 | /* write はバッファリングなしなので flush しない */
|
---|
| 129 |
|
---|
| 130 | return(ercd);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /* #[<ENTRY_FUNC>]# eC0_receive
|
---|
| 134 | * name: eC0_receive
|
---|
| 135 | * global_name: tSocketClient_eC0_receive
|
---|
| 136 | * oneway: false
|
---|
| 137 | * #[</ENTRY_FUNC>]# */
|
---|
| 138 | ER_UINT
|
---|
| 139 | eC0_receive(CELLIDX idx, int8_t* buf, int16_t size, TMO tmo)
|
---|
| 140 | {
|
---|
| 141 | ER ercd = E_OK;
|
---|
| 142 | CELLCB *p_cellcb;
|
---|
| 143 | ssize_t sz;
|
---|
| 144 | if (VALID_IDX(idx)) {
|
---|
| 145 | p_cellcb = GET_CELLCB(idx);
|
---|
| 146 | }
|
---|
| 147 | else {
|
---|
| 148 | return(E_ID);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /* ここに処理本体を記述します */
|
---|
| 152 | sz = read( VAR_sd, buf, (ssize_t)size );
|
---|
| 153 | ercd = errno2ER( errno );
|
---|
| 154 | #ifdef DEBUG
|
---|
| 155 | {
|
---|
| 156 | int i;
|
---|
| 157 | for( i = 0; i < sz; i++ ){
|
---|
| 158 | printf( "%02X ", (unsigned char)buf[i] );
|
---|
| 159 | if( i % 8 == 7 )
|
---|
| 160 | putchar( '\n' );
|
---|
| 161 | }
|
---|
| 162 | putchar( '\n' );
|
---|
| 163 | }
|
---|
| 164 | #endif
|
---|
| 165 | /* mikan 一発で全て読めると仮定 */
|
---|
| 166 | if( sz >= 0 )
|
---|
| 167 | ercd = sz;
|
---|
| 168 |
|
---|
| 169 | return(ercd);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /* #[<ENTRY_FUNC>]# eC0_end_receive
|
---|
| 173 | * name: eC0_end_receive
|
---|
| 174 | * global_name: tSocketClient_eC0_end_receive
|
---|
| 175 | * oneway: false
|
---|
| 176 | * #[</ENTRY_FUNC>]# */
|
---|
| 177 | ER
|
---|
| 178 | eC0_end_receive(CELLIDX idx)
|
---|
| 179 | {
|
---|
| 180 | ER ercd = E_OK;
|
---|
| 181 | CELLCB *p_cellcb;
|
---|
| 182 | if (VALID_IDX(idx)) {
|
---|
| 183 | p_cellcb = GET_CELLCB(idx);
|
---|
| 184 | }
|
---|
| 185 | else {
|
---|
| 186 | return(E_ID);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /* ここに処理本体を記述します #_TEFB_# */
|
---|
| 190 |
|
---|
| 191 | return(ercd);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /* #[<ENTRY_FUNC>]# eC0_reset
|
---|
| 195 | * name: eC0_reset
|
---|
| 196 | * global_name: tSocketClient_eC0_reset
|
---|
| 197 | * oneway: false
|
---|
| 198 | * #[</ENTRY_FUNC>]# */
|
---|
| 199 | ER
|
---|
| 200 | eC0_reset(CELLIDX idx)
|
---|
| 201 | {
|
---|
| 202 | ER ercd = E_OK;
|
---|
| 203 | CELLCB *p_cellcb;
|
---|
| 204 | if (VALID_IDX(idx)) {
|
---|
| 205 | p_cellcb = GET_CELLCB(idx);
|
---|
| 206 | }
|
---|
| 207 | else {
|
---|
| 208 | return(E_ID);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /* ここに処理本体を記述します */
|
---|
| 212 |
|
---|
| 213 | return(ercd);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /* #[<ENTRY_PORT>]# eOpener
|
---|
| 217 | * entry port: eOpener
|
---|
| 218 | * signature: sSocketClientOpener
|
---|
| 219 | * context: task
|
---|
| 220 | * #[</ENTRY_PORT>]# */
|
---|
| 221 |
|
---|
| 222 | /* #[<ENTRY_FUNC>]# eOpener_open
|
---|
| 223 | * name: eOpener_open
|
---|
| 224 | * global_name: tSocketClient_eOpener_open
|
---|
| 225 | * oneway: false
|
---|
| 226 | * #[</ENTRY_FUNC>]# */
|
---|
| 227 | ER
|
---|
| 228 | eOpener_open(CELLIDX idx, const char_t* serverAddr, uint16_t portNo, TMO tmo)
|
---|
| 229 | {
|
---|
| 230 | ER ercd = E_OK;
|
---|
| 231 | CELLCB *p_cellcb;
|
---|
| 232 | int soc;
|
---|
| 233 | struct sockaddr_in addr={AF_INET};
|
---|
| 234 |
|
---|
| 235 | if (VALID_IDX(idx)) {
|
---|
| 236 | p_cellcb = GET_CELLCB(idx);
|
---|
| 237 | }
|
---|
| 238 | else {
|
---|
| 239 | return(E_ID);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /* ここに処理本体を記述します #_TEFB_# */
|
---|
| 243 | soc=socket(AF_INET, SOCK_STREAM, 0);
|
---|
| 244 | if(soc<0){
|
---|
| 245 | ercd = errno2ER( errno );
|
---|
| 246 | syslog( LOG_INFO, "SocketClient: socket failed");
|
---|
| 247 | return ercd;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | #if 0
|
---|
| 251 | /* ノンブロックに変更 */
|
---|
| 252 | int flag=fcntl(soc, F_GETFL, 0);
|
---|
| 253 | if(flag<0) perror("fcntl(GET) error");
|
---|
| 254 | if(fcntl(soc, F_SETFL, flag|O_NONBLOCK)<0) perror("fcntl(NONBLOCK) error");
|
---|
| 255 | #endif /* 0 */
|
---|
| 256 |
|
---|
| 257 | puts( "connecting" );
|
---|
| 258 | inet_aton( serverAddr, &addr.sin_addr );
|
---|
| 259 | addr.sin_port=htons(portNo);
|
---|
| 260 | if(connect(soc, (struct sockaddr*)&addr, sizeof(addr))<0){
|
---|
| 261 | ercd = errno2ER( errno );
|
---|
| 262 | close( soc );
|
---|
| 263 | printf( "connect fail errno=%d, ercd=%d\n", errno, ercd );
|
---|
| 264 | syslog( LOG_INFO, "SocketClient: connect" );
|
---|
| 265 | return ercd;
|
---|
| 266 | }
|
---|
| 267 | VAR_sd = soc;
|
---|
| 268 |
|
---|
| 269 | #if 0
|
---|
| 270 | if(errno!=EINPROGRESS)
|
---|
| 271 | perror("connect error");
|
---|
| 272 | //EINPROGRESS:コネクション要求は始まったが、まだ完了していない
|
---|
| 273 |
|
---|
| 274 | fd_set rmask,wmask;FD_ZERO(&rmask);FD_SET(soc,&rmask);wmask=rmask;
|
---|
| 275 | struct timeval tv={ タイムアウト時間[秒],[μ秒] };
|
---|
| 276 | int rc=select(soc+1, &rmask, &wmask, NULL, &tv);
|
---|
| 277 | if(rc<0) perror("connect-select error");
|
---|
| 278 | if(rc==0){ タイムアウト処理 }
|
---|
| 279 | if(rc==2){ //読み書きが同時に出来る場合
|
---|
| 280 | #if Solaris
|
---|
| 281 | int val;
|
---|
| 282 | socklen_t len=sizeof(val);
|
---|
| 283 | if(getsockopt(soc,SOL_SOCKET,SO_ERROR, &val,&len)>=0) {
|
---|
| 284 | #elif Linux
|
---|
| 285 | struct sockaddr_in name;
|
---|
| 286 | socklen_t len=sizeof(name);
|
---|
| 287 | if(getpeername(soc,(struct sockaddr*)&name,&len)>=0) {
|
---|
| 288 | #endif
|
---|
| 289 | // 既にデータが来ている
|
---|
| 290 | }else{
|
---|
| 291 | // コネクト失敗
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | //フラグを元に戻す
|
---|
| 296 | if(fcntl(soc, F_SETFL, flag)<0) perror("fcntl(END) error");
|
---|
| 297 | #endif /* 0 */
|
---|
| 298 |
|
---|
| 299 | return(ercd);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /* #[<ENTRY_FUNC>]# eOpener_simpleOpen
|
---|
| 303 | * name: eOpener_simpleOpen
|
---|
| 304 | * global_name: tSocketClient_eOpener_simpleOpen
|
---|
| 305 | * oneway: false
|
---|
| 306 | * #[</ENTRY_FUNC>]# */
|
---|
| 307 | ER
|
---|
| 308 | eOpener_simpleOpen(CELLIDX idx, TMO tmo)
|
---|
| 309 | {
|
---|
| 310 | ER ercd = E_OK;
|
---|
| 311 | CELLCB *p_cellcb;
|
---|
| 312 | if (VALID_IDX(idx)) {
|
---|
| 313 | p_cellcb = GET_CELLCB(idx);
|
---|
| 314 | }
|
---|
| 315 | else {
|
---|
| 316 | return(E_ID);
|
---|
| 317 | } /* end if VALID_IDX(idx) */
|
---|
| 318 |
|
---|
| 319 | /* Put statements here #_TEFB_# */
|
---|
| 320 | ercd = eOpener_open(idx, ATTR_serverAddr, ATTR_portNo, tmo);
|
---|
| 321 |
|
---|
| 322 | return(ercd);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /* #[<ENTRY_FUNC>]# eOpener_close
|
---|
| 326 | * name: eOpener_close
|
---|
| 327 | * global_name: tSocketClient_eOpener_close
|
---|
| 328 | * oneway: false
|
---|
| 329 | * #[</ENTRY_FUNC>]# */
|
---|
| 330 | ER
|
---|
| 331 | eOpener_close(CELLIDX idx, TMO tmo)
|
---|
| 332 | {
|
---|
| 333 | ER ercd = E_OK;
|
---|
| 334 | CELLCB *p_cellcb;
|
---|
| 335 | if (VALID_IDX(idx)) {
|
---|
| 336 | p_cellcb = GET_CELLCB(idx);
|
---|
| 337 | }
|
---|
| 338 | else {
|
---|
| 339 | return(E_ID);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /* ここに処理本体を記述します #_TEFB_# */
|
---|
| 343 | close( VAR_sd ); /* mikan エラー処理 */
|
---|
| 344 |
|
---|
| 345 | return(ercd);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | /* #[<POSTAMBLE>]#
|
---|
| 349 | * Put non-entry functions below.
|
---|
| 350 | * #[</POSTAMBLE>]#*/
|
---|