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