123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- /*****************************************************************************/
- /* ここから */
- /*****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include "wonx_include/comm.h"
- #include "wonx_configure.h"
- #include "Wonx.h"
- #include "etc.h"
- /*****************************************************************************/
- /* 互換関数の定義 */
- /*****************************************************************************/
- /*
- * Xサーバとの同期の整合性がとれなくなるなどの問題が考えられるので,
- * 互換関数の内部は UNIXTimer_Pause(), UNIXTimer_Unpause() でくくり,
- * タイマ割り込みを一時停止して処理を行う.また,unpause するまえに,
- * かならず sync するようにする.
- */
- /*
- * タイマの一時停止の2重解除の問題が出てくるので,
- * 互換関数から互換関数を呼んではいけない.
- * (一時停止はネストされるが,いちおう)
- * 似たような処理をする関数の場合は,必ず static な別関数に処理をまとめ,
- * そっちを呼び出すようにすること.
- * 引数の表示の問題もあるしね.
- */
- void comm_open(void)
- {
- WWSerialPort ww_serial_port;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_open() : \n");
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_open", "Serial port has already opened.");
- WWSerialPort_ON(ww_serial_port);
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_open() : return value = none\n");
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return;
- }
- void comm_close(void)
- {
- WWSerialPort ww_serial_port;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_close() : \n");
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_close", "Serial port is not opened.");
- WWSerialPort_OFF(ww_serial_port);
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_close() : return value = none\n");
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return;
- }
- static void comm_output(unsigned char c)
- {
- if (c == '\n') printf("\\n");
- else if (c == '\r') printf("\\r");
- else if (c == '\t') printf("\\t");
- else if (isprint(c)) fputc(c, stdout);
- else printf("^%02x", (int)c);
- fflush(stdout);
- return;
- }
- static int comm_input(int timeout)
- {
- int tmp;
- int c;
- fd_set bitmap;
- struct timeval t;
- struct timeval * t_p;
- /*
- * 0 のときは,即時
- * -1 のときは,無期限待ち
- */
- if (timeout == -1) {
- t_p = NULL;
- } else {
- tmp = WONX_SERIAL_PORT_TIMETICKS * timeout;
- t.tv_sec = tmp / 1000;
- t.tv_usec = (tmp % 1000) * 1000;
- t_p = &t;
- }
- FD_ZERO(&bitmap);
- FD_SET(fileno(stdin), &bitmap);
- /*
- * FreeBSD3.4 で実験したところ,
- * アラームシグナルを使用する場合,select()でのブロック中に
- * アラームシグナルが発生すると,その直後にselect()もタイムアウト
- * してしまうので,注意.
- * (select() がタイムアウトした後にアラームシグナルが発生する場合は,
- * 正常に動作した)
- * 論理上は問題が無いが,期待した時間だけ待ってくれないという現象が
- * 起きる可能性がある.
- */
- select(fileno(stdin) + 1, &bitmap, NULL, NULL, t_p);
- c = FD_ISSET(fileno(stdin), &bitmap) ? fgetc(stdin) : -1;
- c = (c == '\n') ? '\r' : c;
- return (c);
- }
- int comm_send_char(unsigned char c)
- {
- int ret;
- WWSerialPort ww_serial_port;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_send_char() : character = 0x%02x\n", (int)c);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_send_char", "Serial port is not opened.");
- printf("output to serial port : ");
- comm_output(c);
- printf("\n");
- fflush(stdout);
- ret = 0;
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_send_char() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- int comm_receive_char(void)
- {
- WWSerialPort ww_serial_port;
- int c;
- int ret;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_receive_char() : \n");
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_receive_char", "Serial port is not opened.");
- c = comm_input(WWSerialPort_GetReceiveTimeout(ww_serial_port));
- if (c == -1) ret = ERR_SIO_TIMEOUT;
- else ret = c;
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_receive_char() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- int comm_receive_with_timeout(int timeout)
- {
- WWSerialPort ww_serial_port;
- int c;
- int ret;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_receive_with_timeout() : timeout = %d\n", timeout);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_receive_with_timeout", "Serial port is not opened.");
- c = comm_input(timeout);
- if (c == -1) ret = ERR_SIO_TIMEOUT;
- else ret = c;
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_receive_with_timeout() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- int comm_send_string(char * string)
- {
- WWSerialPort ww_serial_port;
- int ret;
- int i;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_send_string() : string = %s\n", string);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_send_string", "Serial port is not opened.");
- printf("output to serial port : ");
- for (i = 0; string[i]; i++) {
- comm_output(string[i]);
- }
- printf("\n");
- fflush(stdout);
- ret = 0;
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_send_string() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- int comm_send_block(void * buffer, int size)
- {
- WWSerialPort ww_serial_port;
- int ret;
- int i;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_send_block() : buffer = %p, size = %d\n", buffer, size);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_send_block", "Serial port is not opened.");
- printf("output to serial port : ");
- for (i = 0; i < size; i++) {
- comm_output(((char *)buffer)[i]);
- }
- printf("\n");
- fflush(stdout);
- ret = 0;
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_send_block() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- int comm_receive_block(void * buffer, int size)
- {
- WWSerialPort ww_serial_port;
- int ret;
- int c;
- int i;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_receive_block() : buffer = %p, size = %d\n",
- buffer, size);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (!WWSerialPort_IsON(ww_serial_port))
- Wonx_Error("comm_receive_block", "Serial port is not opened.");
- ret = 0;
- for (i = 0; i < size; i++) {
- c = comm_input(WWSerialPort_GetReceiveTimeout(ww_serial_port));
- if (c == -1) {
- ret = ERR_SIO_TIMEOUT;
- break;
- }
- ((char *)buffer)[i] = c;
- }
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_receive_block() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- void comm_set_timeout(int recv_timeout, int send_timeout)
- {
- WWSerialPort ww_serial_port;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_set_timeout() : receive_timeout = %d, send_timeout = %d\n", recv_timeout, send_timeout);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- WWSerialPort_SetReceiveTimeout(ww_serial_port, recv_timeout);
- WWSerialPort_SetSendTimeout( ww_serial_port, send_timeout);
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_set_timeout() : return value = none\n");
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return;
- }
- void comm_set_baudrate(int baudrate)
- {
- WWSerialPort ww_serial_port;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_set_baudrate() : baudrate = %d\n", baudrate);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- switch (baudrate) {
- case COMM_SPEED_9600: WWSerialPort_SetBaudrate9600( ww_serial_port); break;
- case COMM_SPEED_38400: WWSerialPort_SetBaudrate38400(ww_serial_port); break;
- default: Wonx_Error("comm_set_baudrate", "Invalid baudrate");
- }
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_set_baudrate() : return value = none\n");
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return;
- }
- int comm_get_baudrate(void)
- {
- WWSerialPort ww_serial_port;
- int ret = 0;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_get_baudrate() : \n");
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- if (WWSerialPort_IsBaudrate9600(ww_serial_port))
- ret = COMM_SPEED_9600;
- else if (WWSerialPort_IsBaudrate38400(ww_serial_port))
- ret = COMM_SPEED_38400;
- else Wonx_Error("comm_get_baudrate", "Invalid baudrate");
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_get_baudrate() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- void comm_set_cancel_key(unsigned int pattern)
- {
- WWSerialPort ww_serial_port;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_set_cancel_key() : pattern = %u\n", (int)pattern);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- WWSerialPort_SetCancelKey(ww_serial_port, pattern);
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_set_cancel_key() : return value = none\n");
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return;
- }
- unsigned int comm_get_cancel_key(void)
- {
- WWSerialPort ww_serial_port;
- unsigned int ret;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_get_cancel_key() : \n");
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- /* ここに処理を書く */
- ret = WWSerialPort_GetCancelKey(ww_serial_port);
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_get_cancel_key() : return value = %u\n", (int)ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- int comm_xmodem(void * xmodem)
- {
- WWSerialPort ww_serial_port;
- int ret;
- if (!Wonx_IsCreated()) Wonx_Create();
- /* タイマを一時停止する */
- UNIXTimer_Pause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- printf("call : comm_xmodem() : xmodem = %p\n", xmodem);
- fflush(stdout);
- ww_serial_port = WonxSerialPort_GetWWSerialPort(Wonx_GetWonxSerialPort());
- /* 未サポート */
- printf("call : comm_xmodem() : not supported.\n");
- fflush(stdout);
- ret = 0;
- WonxDisplay_Sync(Wonx_GetWonxDisplay());
- printf("call : comm_xmodem() : return value = %d\n", ret);
- fflush(stdout);
- /* タイマをもとに戻す */
- UNIXTimer_Unpause(WonxSystem_GetUNIXTimer(Wonx_GetWonxSystem()));
- return (ret);
- }
- /*****************************************************************************/
- /* ここまで */
- /*****************************************************************************/
- /*****************************************************************************/
- /* End of File. */
- /*****************************************************************************/
|