ssssss
CushionSock.cpp@0:01a2e92d2924, 2016-12-17 (annotated)
- Committer:
- 3116redtrain
- Date:
- Sat Dec 17 09:13:37 2016 +0000
- Revision:
- 0:01a2e92d2924
- Child:
- 1:e6addce35f93
first issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
3116redtrain | 0:01a2e92d2924 | 1 | #include "CushionSock.h" |
3116redtrain | 0:01a2e92d2924 | 2 | |
3116redtrain | 0:01a2e92d2924 | 3 | CushionSock::CushionSock(char * hostAndPort){ |
3116redtrain | 0:01a2e92d2924 | 4 | eth = new EthernetNetIf(); |
3116redtrain | 0:01a2e92d2924 | 5 | EthernetErr ethErr = eth->setup(); |
3116redtrain | 0:01a2e92d2924 | 6 | if (ethErr) { |
3116redtrain | 0:01a2e92d2924 | 7 | printf("\r\nERROR %d in setup.\r\n", ethErr); |
3116redtrain | 0:01a2e92d2924 | 8 | } |
3116redtrain | 0:01a2e92d2924 | 9 | printf("hostandport %s\n", hostAndPort); |
3116redtrain | 0:01a2e92d2924 | 10 | |
3116redtrain | 0:01a2e92d2924 | 11 | ws = new Websocket(hostAndPort, eth); |
3116redtrain | 0:01a2e92d2924 | 12 | |
3116redtrain | 0:01a2e92d2924 | 13 | printf("begin\r\n"); |
3116redtrain | 0:01a2e92d2924 | 14 | |
3116redtrain | 0:01a2e92d2924 | 15 | MESS_STOP = 0x01; |
3116redtrain | 0:01a2e92d2924 | 16 | MESS_START = 0x02; |
3116redtrain | 0:01a2e92d2924 | 17 | MESS_SIT = 0x11; |
3116redtrain | 0:01a2e92d2924 | 18 | MESS_FRUST = 0x12; |
3116redtrain | 0:01a2e92d2924 | 19 | MESS_TEST =0x21; |
3116redtrain | 0:01a2e92d2924 | 20 | type_name[MESS_STOP]="stop"; |
3116redtrain | 0:01a2e92d2924 | 21 | type_name[MESS_START]="start"; |
3116redtrain | 0:01a2e92d2924 | 22 | type_name[MESS_SIT]="status_sit"; |
3116redtrain | 0:01a2e92d2924 | 23 | type_name[MESS_FRUST]="status_frust"; |
3116redtrain | 0:01a2e92d2924 | 24 | type_name[MESS_TEST]="test"; |
3116redtrain | 0:01a2e92d2924 | 25 | name_type["stop"]=MESS_STOP; |
3116redtrain | 0:01a2e92d2924 | 26 | name_type["start"]=MESS_START; |
3116redtrain | 0:01a2e92d2924 | 27 | name_type["status_sit"]=MESS_SIT; |
3116redtrain | 0:01a2e92d2924 | 28 | name_type["status_frust"]=MESS_FRUST; |
3116redtrain | 0:01a2e92d2924 | 29 | name_type["test"]=MESS_TEST; |
3116redtrain | 0:01a2e92d2924 | 30 | |
3116redtrain | 0:01a2e92d2924 | 31 | printf("websock init\n"); |
3116redtrain | 0:01a2e92d2924 | 32 | int failcount=0; |
3116redtrain | 0:01a2e92d2924 | 33 | |
3116redtrain | 0:01a2e92d2924 | 34 | while(! ws->connect()) { |
3116redtrain | 0:01a2e92d2924 | 35 | failcount++; |
3116redtrain | 0:01a2e92d2924 | 36 | if(failcount>10){ |
3116redtrain | 0:01a2e92d2924 | 37 | printf("cannot connect failed.\n"); |
3116redtrain | 0:01a2e92d2924 | 38 | break; |
3116redtrain | 0:01a2e92d2924 | 39 | } |
3116redtrain | 0:01a2e92d2924 | 40 | printf("cannot connect websocket, retrying...\n"); |
3116redtrain | 0:01a2e92d2924 | 41 | wait(2); |
3116redtrain | 0:01a2e92d2924 | 42 | } |
3116redtrain | 0:01a2e92d2924 | 43 | } |
3116redtrain | 0:01a2e92d2924 | 44 | |
3116redtrain | 0:01a2e92d2924 | 45 | |
3116redtrain | 0:01a2e92d2924 | 46 | bool CushionSock::connect(){ |
3116redtrain | 0:01a2e92d2924 | 47 | |
3116redtrain | 0:01a2e92d2924 | 48 | return true; |
3116redtrain | 0:01a2e92d2924 | 49 | } |
3116redtrain | 0:01a2e92d2924 | 50 | |
3116redtrain | 0:01a2e92d2924 | 51 | int CushionSock::mess_send(int type, char * msg){ |
3116redtrain | 0:01a2e92d2924 | 52 | string name=type_name[type]; |
3116redtrain | 0:01a2e92d2924 | 53 | MbedJSONValue json; |
3116redtrain | 0:01a2e92d2924 | 54 | json["name"]=name.c_str(); |
3116redtrain | 0:01a2e92d2924 | 55 | json["value"]=msg; |
3116redtrain | 0:01a2e92d2924 | 56 | ws->send((char*)json.serialize().c_str()); |
3116redtrain | 0:01a2e92d2924 | 57 | return 1; |
3116redtrain | 0:01a2e92d2924 | 58 | } |
3116redtrain | 0:01a2e92d2924 | 59 | |
3116redtrain | 0:01a2e92d2924 | 60 | int CushionSock::mess_recv(char * msg){ |
3116redtrain | 0:01a2e92d2924 | 61 | picojson::value v; |
3116redtrain | 0:01a2e92d2924 | 62 | char recv[256]; |
3116redtrain | 0:01a2e92d2924 | 63 | if (ws->read(recv)) { |
3116redtrain | 0:01a2e92d2924 | 64 | int type; |
3116redtrain | 0:01a2e92d2924 | 65 | string name; |
3116redtrain | 0:01a2e92d2924 | 66 | string sbuf(recv); |
3116redtrain | 0:01a2e92d2924 | 67 | const char * crecv=sbuf.c_str(); |
3116redtrain | 0:01a2e92d2924 | 68 | string err = picojson::parse(v,crecv,crecv+strlen(crecv)); |
3116redtrain | 0:01a2e92d2924 | 69 | if(!err.empty()) |
3116redtrain | 0:01a2e92d2924 | 70 | return -1; |
3116redtrain | 0:01a2e92d2924 | 71 | name=v.get("name").get<string>(); |
3116redtrain | 0:01a2e92d2924 | 72 | strncpy(msg,v.get("value").get<string>().c_str(),256); |
3116redtrain | 0:01a2e92d2924 | 73 | type=name_type[name]; |
3116redtrain | 0:01a2e92d2924 | 74 | return type; |
3116redtrain | 0:01a2e92d2924 | 75 | }else{ |
3116redtrain | 0:01a2e92d2924 | 76 | return -1; |
3116redtrain | 0:01a2e92d2924 | 77 | } |
3116redtrain | 0:01a2e92d2924 | 78 | } |
3116redtrain | 0:01a2e92d2924 | 79 | |
3116redtrain | 0:01a2e92d2924 | 80 | bool CushionSock::is_connected(){ |
3116redtrain | 0:01a2e92d2924 | 81 | return ws->connected(); |
3116redtrain | 0:01a2e92d2924 | 82 | } |
3116redtrain | 0:01a2e92d2924 | 83 | |
3116redtrain | 0:01a2e92d2924 | 84 | bool CushionSock::close(){ |
3116redtrain | 0:01a2e92d2924 | 85 | return ws->close(); |
3116redtrain | 0:01a2e92d2924 | 86 | } |