This library contains a simple device driver for the X10 CM17a module. It also contains a simple X10Server, which listens for network commands to be forwarded to the module.

Dependents:   X10Svr

Committer:
WiredHome
Date:
Sat Jun 28 19:45:20 2014 +0000
Revision:
0:12ed8bd4909a
Working X10 Driver for CM17a module.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:12ed8bd4909a 1 #include "X10Server.h"
WiredHome 0:12ed8bd4909a 2
WiredHome 0:12ed8bd4909a 3
WiredHome 0:12ed8bd4909a 4 //#define DEBUG "x10d"
WiredHome 0:12ed8bd4909a 5 #ifdef WIN32
WiredHome 0:12ed8bd4909a 6 #define LF "\n"
WiredHome 0:12ed8bd4909a 7 #else // mbed
WiredHome 0:12ed8bd4909a 8 #define LF "\r\n"
WiredHome 0:12ed8bd4909a 9 #endif // WIN32
WiredHome 0:12ed8bd4909a 10 #include <cstdio>
WiredHome 0:12ed8bd4909a 11 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 0:12ed8bd4909a 12 #define DBG(x, ...) std::printf("[DBG %s %3d] " x LF, DEBUG, __LINE__, ##__VA_ARGS__)
WiredHome 0:12ed8bd4909a 13 #define WARN(x, ...) std::printf("[WRN %s %3d] " x LF, DEBUG, __LINE__, ##__VA_ARGS__)
WiredHome 0:12ed8bd4909a 14 #define ERR(x, ...) std::printf("[ERR %s %3d] " x LF, DEBUG, __LINE__, ##__VA_ARGS__)
WiredHome 0:12ed8bd4909a 15 #define INFO(x, ...) std::printf("[INF %s %3d] " x LF, DEBUG, __LINE__, ##__VA_ARGS__)
WiredHome 0:12ed8bd4909a 16 #else
WiredHome 0:12ed8bd4909a 17 #define DBG(x, ...)
WiredHome 0:12ed8bd4909a 18 #define WARN(x, ...)
WiredHome 0:12ed8bd4909a 19 #define ERR(x, ...)
WiredHome 0:12ed8bd4909a 20 #define INFO(x, ...)
WiredHome 0:12ed8bd4909a 21 #endif
WiredHome 0:12ed8bd4909a 22
WiredHome 0:12ed8bd4909a 23
WiredHome 0:12ed8bd4909a 24 X10Server::X10Server(void(*fptr)(char * buffer, int size), uint16_t port)
WiredHome 0:12ed8bd4909a 25 {
WiredHome 0:12ed8bd4909a 26 listenPort = port;
WiredHome 0:12ed8bd4909a 27 serverIsListening = false;
WiredHome 0:12ed8bd4909a 28 clientIsConnected = false;
WiredHome 0:12ed8bd4909a 29 callback = fptr;
WiredHome 0:12ed8bd4909a 30 INFO("Server binding...");
WiredHome 0:12ed8bd4909a 31 svr.set_blocking(false, 10);
WiredHome 0:12ed8bd4909a 32 if (svr.bind(listenPort)< 0) {
WiredHome 0:12ed8bd4909a 33 WARN("tcp server bind failed.");
WiredHome 0:12ed8bd4909a 34 } else {
WiredHome 0:12ed8bd4909a 35 INFO("tcp server bind successed.");
WiredHome 0:12ed8bd4909a 36 }
WiredHome 0:12ed8bd4909a 37
WiredHome 0:12ed8bd4909a 38 if (svr.listen(1) < 0) {
WiredHome 0:12ed8bd4909a 39 WARN("tcp server listen failed.");
WiredHome 0:12ed8bd4909a 40 } else {
WiredHome 0:12ed8bd4909a 41 INFO("tcp server is listening...");
WiredHome 0:12ed8bd4909a 42 serverIsListening = true;
WiredHome 0:12ed8bd4909a 43 }
WiredHome 0:12ed8bd4909a 44 }
WiredHome 0:12ed8bd4909a 45
WiredHome 0:12ed8bd4909a 46 void X10Server::poll(void)
WiredHome 0:12ed8bd4909a 47 {
WiredHome 0:12ed8bd4909a 48 //listening for a request
WiredHome 0:12ed8bd4909a 49 if (serverIsListening) {
WiredHome 0:12ed8bd4909a 50 //blocking mode(never timeout)
WiredHome 0:12ed8bd4909a 51 if (svr.accept(client)<0) {
WiredHome 0:12ed8bd4909a 52 ; // WARN("failed to accept connection.");
WiredHome 0:12ed8bd4909a 53 } else {
WiredHome 0:12ed8bd4909a 54 INFO("connection success!IP: %s", client.get_address());
WiredHome 0:12ed8bd4909a 55 clientIsConnected = true;
WiredHome 0:12ed8bd4909a 56
WiredHome 0:12ed8bd4909a 57 while (clientIsConnected) {
WiredHome 0:12ed8bd4909a 58 char buffer[1024] = {};
WiredHome 0:12ed8bd4909a 59
WiredHome 0:12ed8bd4909a 60 switch (client.receive(buffer, 1023)) {
WiredHome 0:12ed8bd4909a 61 case 0:
WiredHome 0:12ed8bd4909a 62 INFO("received buffer is empty.");
WiredHome 0:12ed8bd4909a 63 clientIsConnected = false;
WiredHome 0:12ed8bd4909a 64 break;
WiredHome 0:12ed8bd4909a 65 case -1:
WiredHome 0:12ed8bd4909a 66 WARN("failed to read data from client.");
WiredHome 0:12ed8bd4909a 67 clientIsConnected = false;
WiredHome 0:12ed8bd4909a 68 break;
WiredHome 0:12ed8bd4909a 69 default:
WiredHome 0:12ed8bd4909a 70 INFO("Received Data: %d bytes\r\n%s", strlen(buffer), buffer);
WiredHome 0:12ed8bd4909a 71 if (callback)
WiredHome 0:12ed8bd4909a 72 (*callback)(buffer, strlen(buffer));
WiredHome 0:12ed8bd4909a 73 break;
WiredHome 0:12ed8bd4909a 74 }
WiredHome 0:12ed8bd4909a 75 }
WiredHome 0:12ed8bd4909a 76 INFO("close connection, tcp server is listening...");
WiredHome 0:12ed8bd4909a 77 client.close();
WiredHome 0:12ed8bd4909a 78 }
WiredHome 0:12ed8bd4909a 79 }
WiredHome 0:12ed8bd4909a 80 }