
update from matsu
MF_FJK_UDPManager/UDPManager.h@6:fd9d4fed9946, 2019-03-16 (annotated)
- Committer:
- shinyamatsuyama
- Date:
- Sat Mar 16 15:42:24 2019 +0000
- Revision:
- 6:fd9d4fed9946
- Parent:
- 2:89530618a5c7
091_sendfix; 11_udpread
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
poipoi | 2:89530618a5c7 | 1 | #pragma once |
poipoi | 2:89530618a5c7 | 2 | |
poipoi | 2:89530618a5c7 | 3 | #include "mbed.h" |
poipoi | 2:89530618a5c7 | 4 | #include "EthernetInterface.h" |
poipoi | 2:89530618a5c7 | 5 | |
poipoi | 2:89530618a5c7 | 6 | |
poipoi | 2:89530618a5c7 | 7 | enum MessageID { |
poipoi | 2:89530618a5c7 | 8 | MESSAGE_A = 0, |
poipoi | 2:89530618a5c7 | 9 | MESSAGE_B, |
poipoi | 2:89530618a5c7 | 10 | MESSAGE_C, |
poipoi | 2:89530618a5c7 | 11 | MESSAGE_D, |
poipoi | 2:89530618a5c7 | 12 | MESSAGE_E, |
shinyamatsuyama | 6:fd9d4fed9946 | 13 | MESSAGE_F, |
shinyamatsuyama | 6:fd9d4fed9946 | 14 | MESSAGE_G, |
shinyamatsuyama | 6:fd9d4fed9946 | 15 | MESSAGE_H, |
poipoi | 2:89530618a5c7 | 16 | MESSAGE_NONE = -1 |
poipoi | 2:89530618a5c7 | 17 | }; |
poipoi | 2:89530618a5c7 | 18 | |
poipoi | 2:89530618a5c7 | 19 | enum ColorID { |
poipoi | 2:89530618a5c7 | 20 | COLOR_RED = 0, |
poipoi | 2:89530618a5c7 | 21 | COLOR_BLUE, |
poipoi | 2:89530618a5c7 | 22 | COLOR_GREEN, |
poipoi | 2:89530618a5c7 | 23 | COLOR_YELLOW, |
poipoi | 2:89530618a5c7 | 24 | COLOR_NONE = -1 |
poipoi | 2:89530618a5c7 | 25 | }; |
poipoi | 2:89530618a5c7 | 26 | |
poipoi | 2:89530618a5c7 | 27 | enum CourseID { |
poipoi | 2:89530618a5c7 | 28 | COURSE_LEFT = 0, |
poipoi | 2:89530618a5c7 | 29 | COURSE_CENTER, |
poipoi | 2:89530618a5c7 | 30 | COURSE_RIGHT, |
poipoi | 2:89530618a5c7 | 31 | COURSE_TOP, |
poipoi | 2:89530618a5c7 | 32 | COURSE_MIDDLE, |
poipoi | 2:89530618a5c7 | 33 | COURSE_BOTTOM, |
poipoi | 2:89530618a5c7 | 34 | COURSE_NONE = -1 |
poipoi | 2:89530618a5c7 | 35 | }; |
poipoi | 2:89530618a5c7 | 36 | |
poipoi | 2:89530618a5c7 | 37 | class UDPManager { |
poipoi | 2:89530618a5c7 | 38 | private: |
poipoi | 2:89530618a5c7 | 39 | EthernetInterface eth; |
poipoi | 2:89530618a5c7 | 40 | UDPSocket sock; |
poipoi | 2:89530618a5c7 | 41 | SocketAddress adr; |
poipoi | 2:89530618a5c7 | 42 | char sendBuff[256]; |
poipoi | 2:89530618a5c7 | 43 | char recBuff[256]; |
poipoi | 2:89530618a5c7 | 44 | bool isOpen; |
poipoi | 2:89530618a5c7 | 45 | uint32_t index; |
poipoi | 2:89530618a5c7 | 46 | |
poipoi | 2:89530618a5c7 | 47 | void sendMessage(MessageID msgID, ColorID colorID, CourseID courseID = COURSE_NONE, int contentID = -1) { |
poipoi | 2:89530618a5c7 | 48 | memset(sendBuff, '\0', sizeof(sendBuff)); |
poipoi | 2:89530618a5c7 | 49 | |
poipoi | 2:89530618a5c7 | 50 | if (msgID == MESSAGE_NONE) return; |
poipoi | 2:89530618a5c7 | 51 | switch (msgID) { |
poipoi | 2:89530618a5c7 | 52 | case MESSAGE_A: |
poipoi | 2:89530618a5c7 | 53 | strcat(sendBuff, "01"); |
poipoi | 2:89530618a5c7 | 54 | break; |
poipoi | 2:89530618a5c7 | 55 | case MESSAGE_B: |
poipoi | 2:89530618a5c7 | 56 | strcat(sendBuff, "02"); |
poipoi | 2:89530618a5c7 | 57 | break; |
poipoi | 2:89530618a5c7 | 58 | case MESSAGE_C: |
poipoi | 2:89530618a5c7 | 59 | strcat(sendBuff, "03"); |
poipoi | 2:89530618a5c7 | 60 | break; |
poipoi | 2:89530618a5c7 | 61 | case MESSAGE_D: |
poipoi | 2:89530618a5c7 | 62 | strcat(sendBuff, "04"); |
poipoi | 2:89530618a5c7 | 63 | break; |
poipoi | 2:89530618a5c7 | 64 | } |
poipoi | 2:89530618a5c7 | 65 | |
poipoi | 2:89530618a5c7 | 66 | // TimeStamp |
poipoi | 2:89530618a5c7 | 67 | sprintf(sendBuff, "%s,%d", sendBuff, index++); |
poipoi | 2:89530618a5c7 | 68 | |
poipoi | 2:89530618a5c7 | 69 | if (colorID == COLOR_NONE) return; |
poipoi | 2:89530618a5c7 | 70 | switch (colorID) { |
poipoi | 2:89530618a5c7 | 71 | case COLOR_RED: |
poipoi | 2:89530618a5c7 | 72 | strcat(sendBuff, ",R"); |
poipoi | 2:89530618a5c7 | 73 | break; |
poipoi | 2:89530618a5c7 | 74 | case COLOR_BLUE: |
poipoi | 2:89530618a5c7 | 75 | strcat(sendBuff, ",B"); |
poipoi | 2:89530618a5c7 | 76 | break; |
poipoi | 2:89530618a5c7 | 77 | case COLOR_GREEN: |
poipoi | 2:89530618a5c7 | 78 | strcat(sendBuff, ",G"); |
poipoi | 2:89530618a5c7 | 79 | break; |
poipoi | 2:89530618a5c7 | 80 | case COLOR_YELLOW: |
poipoi | 2:89530618a5c7 | 81 | strcat(sendBuff, ",Y"); |
poipoi | 2:89530618a5c7 | 82 | break; |
poipoi | 2:89530618a5c7 | 83 | } |
poipoi | 2:89530618a5c7 | 84 | |
poipoi | 2:89530618a5c7 | 85 | if (courseID != COURSE_NONE) { |
poipoi | 2:89530618a5c7 | 86 | switch (courseID) { |
poipoi | 2:89530618a5c7 | 87 | case COURSE_LEFT: |
poipoi | 2:89530618a5c7 | 88 | strcat(sendBuff, ",L"); |
poipoi | 2:89530618a5c7 | 89 | break; |
poipoi | 2:89530618a5c7 | 90 | case COURSE_CENTER: |
poipoi | 2:89530618a5c7 | 91 | strcat(sendBuff, ",C"); |
poipoi | 2:89530618a5c7 | 92 | break; |
poipoi | 2:89530618a5c7 | 93 | case COURSE_RIGHT: |
poipoi | 2:89530618a5c7 | 94 | strcat(sendBuff, ",R"); |
poipoi | 2:89530618a5c7 | 95 | break; |
poipoi | 2:89530618a5c7 | 96 | case COURSE_TOP: |
poipoi | 2:89530618a5c7 | 97 | strcat(sendBuff, ",T"); |
poipoi | 2:89530618a5c7 | 98 | break; |
poipoi | 2:89530618a5c7 | 99 | case COURSE_MIDDLE: |
poipoi | 2:89530618a5c7 | 100 | strcat(sendBuff, ",M"); |
poipoi | 2:89530618a5c7 | 101 | break; |
poipoi | 2:89530618a5c7 | 102 | case COURSE_BOTTOM: |
poipoi | 2:89530618a5c7 | 103 | strcat(sendBuff, ",B"); |
poipoi | 2:89530618a5c7 | 104 | break; |
poipoi | 2:89530618a5c7 | 105 | } |
poipoi | 2:89530618a5c7 | 106 | } |
poipoi | 2:89530618a5c7 | 107 | |
poipoi | 2:89530618a5c7 | 108 | if (contentID >= 0) { |
poipoi | 2:89530618a5c7 | 109 | sprintf(sendBuff, "%s,%d", sendBuff, contentID); |
poipoi | 2:89530618a5c7 | 110 | } |
poipoi | 2:89530618a5c7 | 111 | |
poipoi | 2:89530618a5c7 | 112 | nsapi_size_or_error_t err = sock.sendto(adr, sendBuff, strlen(sendBuff)); |
poipoi | 2:89530618a5c7 | 113 | if (err < 0) { |
poipoi | 2:89530618a5c7 | 114 | printf("sock.sendto error: %d\n", err); |
poipoi | 2:89530618a5c7 | 115 | } |
poipoi | 2:89530618a5c7 | 116 | } |
poipoi | 2:89530618a5c7 | 117 | |
poipoi | 2:89530618a5c7 | 118 | public: |
poipoi | 2:89530618a5c7 | 119 | UDPManager() : adr("255.255.255.255", 12345), isOpen(false), index(0) {} |
poipoi | 2:89530618a5c7 | 120 | |
poipoi | 2:89530618a5c7 | 121 | bool init() { |
poipoi | 2:89530618a5c7 | 122 | isOpen = false; |
poipoi | 2:89530618a5c7 | 123 | |
poipoi | 2:89530618a5c7 | 124 | int err = eth.connect(); |
poipoi | 2:89530618a5c7 | 125 | if (err != 0) return false; |
poipoi | 2:89530618a5c7 | 126 | |
poipoi | 2:89530618a5c7 | 127 | sock.open(ð); |
poipoi | 2:89530618a5c7 | 128 | sock.set_blocking(false); |
poipoi | 2:89530618a5c7 | 129 | sock.bind(12345); |
poipoi | 2:89530618a5c7 | 130 | |
poipoi | 2:89530618a5c7 | 131 | isOpen = true; |
poipoi | 2:89530618a5c7 | 132 | return true; |
poipoi | 2:89530618a5c7 | 133 | } |
poipoi | 2:89530618a5c7 | 134 | |
poipoi | 2:89530618a5c7 | 135 | bool init(const char* ip, const char* mask, const char* gateway) { |
poipoi | 2:89530618a5c7 | 136 | isOpen = false; |
poipoi | 2:89530618a5c7 | 137 | eth.set_network(ip, mask, gateway); |
poipoi | 2:89530618a5c7 | 138 | int err = eth.connect(); |
poipoi | 2:89530618a5c7 | 139 | if (err != 0) return false; |
poipoi | 2:89530618a5c7 | 140 | |
poipoi | 2:89530618a5c7 | 141 | sock.open(ð); |
poipoi | 2:89530618a5c7 | 142 | sock.set_blocking(false); |
poipoi | 2:89530618a5c7 | 143 | sock.bind(12345); |
poipoi | 2:89530618a5c7 | 144 | |
poipoi | 2:89530618a5c7 | 145 | isOpen = true; |
poipoi | 2:89530618a5c7 | 146 | return true; |
poipoi | 2:89530618a5c7 | 147 | } |
poipoi | 2:89530618a5c7 | 148 | |
poipoi | 2:89530618a5c7 | 149 | const char* getIPAdr() { |
poipoi | 2:89530618a5c7 | 150 | if (!isOpen) return ""; |
poipoi | 2:89530618a5c7 | 151 | return eth.get_ip_address(); |
poipoi | 2:89530618a5c7 | 152 | } |
poipoi | 2:89530618a5c7 | 153 | |
poipoi | 2:89530618a5c7 | 154 | const char* getLatestMessage() { |
poipoi | 2:89530618a5c7 | 155 | return sendBuff; |
poipoi | 2:89530618a5c7 | 156 | } |
poipoi | 2:89530618a5c7 | 157 | |
poipoi | 2:89530618a5c7 | 158 | bool sendA(ColorID colorID) { |
poipoi | 2:89530618a5c7 | 159 | if (!isOpen) return false; |
poipoi | 2:89530618a5c7 | 160 | |
poipoi | 2:89530618a5c7 | 161 | sendMessage(MESSAGE_A, colorID); |
poipoi | 2:89530618a5c7 | 162 | return true; |
poipoi | 2:89530618a5c7 | 163 | } |
poipoi | 2:89530618a5c7 | 164 | |
poipoi | 2:89530618a5c7 | 165 | bool sendB(ColorID colorID, CourseID courseID, int contentID) { |
poipoi | 2:89530618a5c7 | 166 | if (!isOpen) return false; |
poipoi | 2:89530618a5c7 | 167 | |
poipoi | 2:89530618a5c7 | 168 | sendMessage(MESSAGE_B, colorID, courseID, contentID); |
poipoi | 2:89530618a5c7 | 169 | return true; |
poipoi | 2:89530618a5c7 | 170 | } |
poipoi | 2:89530618a5c7 | 171 | |
poipoi | 2:89530618a5c7 | 172 | bool sendC(ColorID colorID) { |
poipoi | 2:89530618a5c7 | 173 | if (!isOpen) return false; |
poipoi | 2:89530618a5c7 | 174 | |
poipoi | 2:89530618a5c7 | 175 | sendMessage(MESSAGE_C, colorID); |
poipoi | 2:89530618a5c7 | 176 | return true; |
poipoi | 2:89530618a5c7 | 177 | } |
poipoi | 2:89530618a5c7 | 178 | |
poipoi | 2:89530618a5c7 | 179 | bool sendD(ColorID colorID, CourseID courseID) { |
poipoi | 2:89530618a5c7 | 180 | if (!isOpen) return false; |
poipoi | 2:89530618a5c7 | 181 | |
poipoi | 2:89530618a5c7 | 182 | sendMessage(MESSAGE_D, colorID, courseID); |
poipoi | 2:89530618a5c7 | 183 | return true; |
poipoi | 2:89530618a5c7 | 184 | } |
poipoi | 2:89530618a5c7 | 185 | |
poipoi | 2:89530618a5c7 | 186 | bool receive(MessageID &msgID, int &timeStamp, ColorID &colorID, CourseID &courseID) { |
poipoi | 2:89530618a5c7 | 187 | msgID = MESSAGE_NONE; |
poipoi | 2:89530618a5c7 | 188 | timeStamp = 0; |
poipoi | 2:89530618a5c7 | 189 | colorID = COLOR_NONE; |
poipoi | 2:89530618a5c7 | 190 | courseID = COURSE_NONE; |
poipoi | 2:89530618a5c7 | 191 | |
poipoi | 2:89530618a5c7 | 192 | SocketAddress recAdr; |
poipoi | 2:89530618a5c7 | 193 | nsapi_size_or_error_t result = sock.recvfrom(&recAdr, recBuff, sizeof(recBuff)); |
poipoi | 2:89530618a5c7 | 194 | |
poipoi | 2:89530618a5c7 | 195 | if (result < 0) { |
poipoi | 2:89530618a5c7 | 196 | if (result != NSAPI_ERROR_WOULD_BLOCK) { |
poipoi | 2:89530618a5c7 | 197 | printf("sock.recvfrom error: %d\n", result); |
poipoi | 2:89530618a5c7 | 198 | } |
poipoi | 2:89530618a5c7 | 199 | return false; |
poipoi | 2:89530618a5c7 | 200 | } |
poipoi | 2:89530618a5c7 | 201 | recBuff[result] = '\0'; |
poipoi | 2:89530618a5c7 | 202 | |
poipoi | 2:89530618a5c7 | 203 | char* tmp = strtok(recBuff, ","); |
poipoi | 2:89530618a5c7 | 204 | if (strcmp(tmp, "01") == 0) { |
poipoi | 2:89530618a5c7 | 205 | msgID = MESSAGE_A; |
poipoi | 2:89530618a5c7 | 206 | } else if (strcmp(tmp, "02") == 0) { |
poipoi | 2:89530618a5c7 | 207 | msgID = MESSAGE_B; |
poipoi | 2:89530618a5c7 | 208 | } else if (strcmp(tmp, "03") == 0) { |
poipoi | 2:89530618a5c7 | 209 | msgID = MESSAGE_C; |
poipoi | 2:89530618a5c7 | 210 | } else if (strcmp(tmp, "04") == 0) { |
poipoi | 2:89530618a5c7 | 211 | msgID = MESSAGE_D; |
poipoi | 2:89530618a5c7 | 212 | } else if (strcmp(tmp, "05") == 0) { |
poipoi | 2:89530618a5c7 | 213 | msgID = MESSAGE_E; |
shinyamatsuyama | 6:fd9d4fed9946 | 214 | } else if (strcmp(tmp, "08") == 0) { |
shinyamatsuyama | 6:fd9d4fed9946 | 215 | msgID = MESSAGE_H; |
shinyamatsuyama | 6:fd9d4fed9946 | 216 | return true; |
poipoi | 2:89530618a5c7 | 217 | } else { |
poipoi | 2:89530618a5c7 | 218 | msgID = MESSAGE_NONE; |
poipoi | 2:89530618a5c7 | 219 | return false; |
poipoi | 2:89530618a5c7 | 220 | } |
poipoi | 2:89530618a5c7 | 221 | |
poipoi | 2:89530618a5c7 | 222 | // TBD: TimeStamp |
poipoi | 2:89530618a5c7 | 223 | tmp = strtok(NULL, ","); |
poipoi | 2:89530618a5c7 | 224 | |
poipoi | 2:89530618a5c7 | 225 | tmp = strtok(NULL, ","); |
poipoi | 2:89530618a5c7 | 226 | switch (msgID) { |
poipoi | 2:89530618a5c7 | 227 | case MESSAGE_A: |
poipoi | 2:89530618a5c7 | 228 | case MESSAGE_B: |
poipoi | 2:89530618a5c7 | 229 | case MESSAGE_C: |
poipoi | 2:89530618a5c7 | 230 | case MESSAGE_D: |
poipoi | 2:89530618a5c7 | 231 | if (strcmp(tmp, "R") == 0) { |
poipoi | 2:89530618a5c7 | 232 | colorID = COLOR_RED; |
poipoi | 2:89530618a5c7 | 233 | } else if (strcmp(tmp, "B") == 0) { |
poipoi | 2:89530618a5c7 | 234 | colorID = COLOR_BLUE; |
poipoi | 2:89530618a5c7 | 235 | } else if (strcmp(tmp, "G") == 0) { |
poipoi | 2:89530618a5c7 | 236 | colorID = COLOR_GREEN; |
poipoi | 2:89530618a5c7 | 237 | } else if (strcmp(tmp, "Y") == 0) { |
poipoi | 2:89530618a5c7 | 238 | colorID = COLOR_YELLOW; |
poipoi | 2:89530618a5c7 | 239 | } else { |
poipoi | 2:89530618a5c7 | 240 | msgID = MESSAGE_NONE; |
poipoi | 2:89530618a5c7 | 241 | return false; |
poipoi | 2:89530618a5c7 | 242 | } |
poipoi | 2:89530618a5c7 | 243 | break; |
poipoi | 2:89530618a5c7 | 244 | |
poipoi | 2:89530618a5c7 | 245 | case MESSAGE_E: |
poipoi | 2:89530618a5c7 | 246 | // TBD |
poipoi | 2:89530618a5c7 | 247 | break; |
shinyamatsuyama | 6:fd9d4fed9946 | 248 | case MESSAGE_H: |
shinyamatsuyama | 6:fd9d4fed9946 | 249 | // no param |
shinyamatsuyama | 6:fd9d4fed9946 | 250 | break; |
poipoi | 2:89530618a5c7 | 251 | } |
poipoi | 2:89530618a5c7 | 252 | |
poipoi | 2:89530618a5c7 | 253 | tmp = strtok(NULL, ","); |
poipoi | 2:89530618a5c7 | 254 | switch (msgID) { |
poipoi | 2:89530618a5c7 | 255 | case MESSAGE_B: |
poipoi | 2:89530618a5c7 | 256 | case MESSAGE_C: |
poipoi | 2:89530618a5c7 | 257 | case MESSAGE_D: |
poipoi | 2:89530618a5c7 | 258 | if (strcmp(tmp, "L") == 0) { |
poipoi | 2:89530618a5c7 | 259 | courseID = COURSE_LEFT; |
poipoi | 2:89530618a5c7 | 260 | } else if (strcmp(tmp, "C") == 0) { |
poipoi | 2:89530618a5c7 | 261 | courseID = COURSE_CENTER; |
poipoi | 2:89530618a5c7 | 262 | } else if (strcmp(tmp, "R") == 0) { |
poipoi | 2:89530618a5c7 | 263 | courseID = COURSE_RIGHT; |
poipoi | 2:89530618a5c7 | 264 | } else if (strcmp(tmp, "T") == 0) { |
poipoi | 2:89530618a5c7 | 265 | courseID = COURSE_TOP; |
poipoi | 2:89530618a5c7 | 266 | } else if (strcmp(tmp, "M") == 0) { |
poipoi | 2:89530618a5c7 | 267 | courseID = COURSE_MIDDLE; |
poipoi | 2:89530618a5c7 | 268 | } else if (strcmp(tmp, "B") == 0) { |
poipoi | 2:89530618a5c7 | 269 | courseID = COURSE_BOTTOM; |
poipoi | 2:89530618a5c7 | 270 | } else { |
poipoi | 2:89530618a5c7 | 271 | msgID = MESSAGE_NONE; |
poipoi | 2:89530618a5c7 | 272 | return false; |
poipoi | 2:89530618a5c7 | 273 | } |
poipoi | 2:89530618a5c7 | 274 | break; |
poipoi | 2:89530618a5c7 | 275 | } |
poipoi | 2:89530618a5c7 | 276 | |
poipoi | 2:89530618a5c7 | 277 | return true; |
poipoi | 2:89530618a5c7 | 278 | } |
poipoi | 2:89530618a5c7 | 279 | }; |