packetHandleTarget.c

Go to the documentation of this file.
00001 
00010 #include "packetHandleTarget.h"
00011 //#include "structTables.h"
00012 #include "nodeAccess.h"
00013 #include "transferCtrl.h"
00014 #include "tRunCtrl.h"
00015 
00016 
00017 int packetRecvHandler(runCtrl_t *run) {
00018   enum {
00019     WaitStartByte,
00020     WaitHeaderRecv,
00021     WaitPacketRecv,
00022     RecvCompleted,
00023     InvalidPacket,
00024 
00025     RecvPacketSize = 512,
00026   };
00027   static int state = InvalidPacket;
00028   static unsigned char recv_buffer[RecvPacketSize];
00029   static int require_size = 0;
00030   static int filled = 0;
00031   static int type = 0;
00032   static int packet_length = 0;
00033   int n;
00034 
00035   // !!! 処理の開始から return まで、1[msec] の割り込みを禁止すべきな気がする
00036 
00037   while (1) {
00038     if (state == InvalidPacket) {
00039       // 受信情報の初期化
00040       state = WaitStartByte;
00041       require_size = NODE_ACCESS_PACKET_HEADER_SIZE;
00042       recv_buffer[0] = 0x00;
00043       filled = removeInvalidPacketHeader((unsigned char*)recv_buffer, filled);
00044     }
00045 
00046     // 受信処理
00047     filled = removeInvalidPacketHeader(recv_buffer, filled);
00048 
00049     // V コマンドへの応答
00050     if ((state == WaitStartByte) && (trans_size() >= 2)) {
00051       unsigned char command[2] = { '\0', '\0' };
00052       trans_copy(command, 2);
00053       if ((command[0] == 'V') &&
00054           ((command[1] == '\n') || (command[1] == '\r'))) {
00055         // バージョン情報を返す
00056         trans_recv(command, 2);
00057         trans_send((unsigned char *)V_COMMAND_REPLY, sizeof(V_COMMAND_REPLY));
00058       }
00059     }
00060 
00061     if (trans_size() < require_size) {
00062       return 0;
00063     }
00064     n = trans_recv(&recv_buffer[filled], require_size);
00065     filled += n;
00066     require_size -= n;
00067 
00068     filled = removeInvalidPacketHeader(recv_buffer, filled);
00069 
00070     if (state == WaitStartByte) {
00071       // スタートバイト待ち
00072       if ((filled >= 3) && (checkFirstTag(recv_buffer) >= 0)) {
00073         state = WaitHeaderRecv;
00074       }
00075     }
00076 
00077     if (state == WaitHeaderRecv) {
00078       // ヘッダのチェックサム確認
00079       if (checkHeaderFormat(recv_buffer) < 0) {
00080         state = InvalidPacket;
00081       } else {
00082         // ヘッダからパケット情報を取得
00083         packet_length = getPacketLength(recv_buffer);
00084         require_size += packet_length - NODE_ACCESS_PACKET_HEADER_SIZE;
00085         state = WaitPacketRecv;
00086         type = getPacketType(recv_buffer);
00087       }
00088       continue;
00089     }
00090 
00091     if (state == WaitPacketRecv) {
00092       if (checkPacketFormat(recv_buffer, packet_length) < 0) {
00093         state = InvalidPacket;
00094       } else {
00095         state = RecvCompleted;
00096       }
00097     }
00098 
00099     if (state == RecvCompleted) {
00100       if (type == PACKET_WRITE_REQUEST) {
00101         // 書き込みパケットへの応答
00102         writeFromPacketData(recv_buffer, (unsigned char *)run);
00103         packet_length =
00104           createWriteResponsePacket(recv_buffer, (unsigned char *)run);
00105 
00106       } else if (type == PACKET_READ_REQUEST) {
00107         // 読み出しパケットへの応答
00108         packet_length =
00109           createReadResponsePacket(recv_buffer, (unsigned char *)run);
00110       } else {
00111         packet_length = 0;
00112       }
00113       trans_send(recv_buffer, packet_length);
00114       state = InvalidPacket;
00115       filled = 0;
00116       return 1;
00117     }
00118   }
00119 }
00120 

Generated on Mon Apr 13 22:52:04 2009 by  doxygen 1.5.7.1