network.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <sys/confnet.h>
  8. #include <sys/socket.h>
  9. #include <netinet/tcp.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <net/route.h>
  14. #include <dev/nicrtl.h>
  15. #include <stdio.h>
  16. #include <io.h>
  17. #include <arpa/inet.h>
  18. #include <pro/dhcp.h>
  19. #include <pro/sntp.h>
  20. #include "ntp.h"
  21. #include "network.h"
  22. #include "jsmn.h"
  23. #include "rtc.h"
  24. #include "alarm.h"
  25. #include "buttonPrefix.h"
  26. bool isReceiving;
  27. bool hasNetwork;
  28. void NetworkInit() {
  29. hasNetwork = false;
  30. /* Register de internet controller. */
  31. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  32. printf("Registering failed. \n");
  33. }/* Netwerk configureren op dhcp */
  34. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  35. printf("DHCP failed. \n");
  36. }else {
  37. printf("Ik heb een internet connectie. Ip is: %s \nMac address is: %s\n\n", inet_ntoa(confnet.cdn_ip_addr), ether_ntoa(confnet.cdn_mac));
  38. }
  39. NutSleep(2000);
  40. hasNetwork = true;
  41. }
  42. char* getMacAdress(){
  43. ether_ntoa(confnet.cdn_mac);
  44. }
  45. char* httpGet(char address[]){
  46. isReceiving = true;
  47. printf("\n\n #-- HTTP get -- #\n");
  48. TCPSOCKET* sock = NutTcpCreateSocket();
  49. char buffer[2];
  50. char* content = (char*) calloc(1 , 800);
  51. char enters = 0;
  52. int t = 0;
  53. if(content == 0){
  54. printf("Can't calloc memory\n");
  55. }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  56. printf("Can't connect to server\n");
  57. }else{
  58. FILE *stream;
  59. stream = _fdopen((int) sock, "r+b");
  60. //Writing http GET to stream
  61. fprintf(stream, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  62. fflush(stream);
  63. printf("Headers writed. Now reading.");
  64. NutDelay(500);
  65. //Removing header:
  66. while(fgets(buffer, sizeof(buffer), stream) != NULL) {
  67. if(enters == 4) {
  68. //printf("\nbuffer : %s \n", buffer);
  69. content[t] = buffer[0];
  70. t++;
  71. }else {
  72. if (buffer[0] == '\n' || buffer[0] == '\r') {
  73. enters++;
  74. }
  75. else {
  76. enters = 0;
  77. }
  78. }
  79. }
  80. fclose(stream);
  81. }
  82. NutTcpCloseSocket(sock);
  83. content[t] = '\0';
  84. printf("\nContent size: %d, Content: %s \n", t, content);
  85. isReceiving = false;
  86. return content;
  87. }
  88. int getTimeZone()
  89. {
  90. char* content = httpGet("/gettimezone.php");
  91. int timezone = atoi(content);
  92. free(content);
  93. printf("%d", timezone);
  94. return timezone;
  95. }
  96. void parseAlarmJson(char* content){
  97. int r;
  98. int i;
  99. jsmn_parser p;
  100. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  101. jsmn_init(&p);
  102. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  103. if (r <= 0) {
  104. printf("Failed to parse JSON: %d \n", r);
  105. return;
  106. }else{
  107. printf("Aantal tokens found: %d \n", r);
  108. }
  109. int start = 1;
  110. int usedAlarms[maxAlarms()];
  111. int j;
  112. for(j = 0; j < maxAlarms(); j++){
  113. usedAlarms[j] = 0;
  114. }
  115. for(i = 1; i < r; i++)
  116. {
  117. struct _tm time = GetRTCTime();
  118. u_short port;
  119. char url[24];
  120. char ip[24];
  121. char name[16];
  122. memset(url, 0, 24);
  123. memset(ip, 0, 24);
  124. memset(name, 0, 16);
  125. int id;
  126. for (i = i; !((i + start) % 24 == 0); i++) {
  127. if (jsoneq(content, &token[i], "YYYY") == 0) {
  128. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  129. i++;
  130. }else if (jsoneq(content, &token[i], "MM") == 0) {
  131. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  132. i++;
  133. }else if (jsoneq(content, &token[i], "DD") == 0) {
  134. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  135. i++;
  136. }else if (jsoneq(content, &token[i], "hh") == 0) {
  137. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  138. i++;
  139. }else if (jsoneq(content, &token[i], "mm") == 0) {
  140. time.tm_min = getIntegerToken(content, &token[i + 1]);
  141. i++;
  142. }else if (jsoneq(content, &token[i], "ss") == 0) {
  143. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  144. i++;
  145. }else if (jsoneq(content, &token[i], "id") == 0) {
  146. id = getIntegerToken(content, &token[i + 1]);
  147. i++;
  148. }else if (jsoneq(content, &token[i], "port") == 0) {
  149. port = getIntegerToken(content, &token[i + 1]);
  150. i++;
  151. }else if (jsoneq(content, &token[i], "ip") == 0) {
  152. getStringToken(content, &token[i + 1], ip);
  153. i++;
  154. }else if (jsoneq(content, &token[i], "url") == 0) {
  155. getStringToken(content, &token[i + 1], url);
  156. i++;
  157. }else if (jsoneq(content, &token[i], "name") == 0) {
  158. getStringToken(content, &token[i + 1], name);
  159. i++;
  160. }
  161. }
  162. start = 0;
  163. int idx = alarmExist(id);
  164. if(idx == -1){
  165. printf("New alarm found!\n");
  166. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  167. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  168. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  169. printf("Alarm id and name is: %d %s\n\n", id, name);
  170. //zoek naar een vrije plaats in de alarm array
  171. for(j = 0; j < maxAlarms(); j++){
  172. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  173. setAlarm(time, name, ip, port, url, 5, id, j);
  174. usedAlarms[j] = 1;
  175. j = 10;
  176. }
  177. }
  178. }else{
  179. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  180. }
  181. }
  182. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  183. if(usedAlarms[j] == 0){
  184. deleteAlarm(j);
  185. };
  186. }
  187. }
  188. void parsePrefixJson(char* content){
  189. int r;
  190. int i;
  191. jsmn_parser p;
  192. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  193. jsmn_init(&p);
  194. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  195. if (r <= 0) {
  196. printf("Failed to parse JSON: %d \n", r);
  197. return;
  198. }else{
  199. printf("Aantal tokens found prefix: %d \n", r);
  200. }
  201. int start = 1;
  202. //int usedAlarms[maxAlarms()];
  203. //int j;
  204. //for(j = 0; j < maxAlarms(); j++){
  205. // usedAlarms[j] = 0;
  206. //}
  207. for(i = 1; i < r; i++)
  208. {
  209. struct _LeftButtons something;
  210. u_short id;
  211. u_short bid;
  212. char name[16];
  213. char ip[24];
  214. u_short port;
  215. char url[24];
  216. memset(url, 0, 24);
  217. memset(ip, 0, 24);
  218. memset(name, 0, 16);
  219. // for (i = i; !((i + start) % 24 == 0); i++) {
  220. // if (jsoneq(content, &token[i], "YYYY") == 0) {
  221. // time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  222. // i++;
  223. // }else if (jsoneq(content, &token[i], "MM") == 0) {
  224. // time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  225. // i++;
  226. // }else if (jsoneq(content, &token[i], "DD") == 0) {
  227. // time.tm_mday = getIntegerToken(content, &token[i + 1]);
  228. // i++;
  229. // }else if (jsoneq(content, &token[i], "hh") == 0) {
  230. // time.tm_hour = getIntegerToken(content, &token[i + 1]);
  231. // i++;
  232. // }else if (jsoneq(content, &token[i], "mm") == 0) {
  233. // time.tm_min = getIntegerToken(content, &token[i + 1]);
  234. // i++;
  235. // }else if (jsoneq(content, &token[i], "ss") == 0) {
  236. // time.tm_sec = getIntegerToken(content, &token[i + 1]);
  237. // i++;
  238. // }else if (jsoneq(content, &token[i], "id") == 0) {
  239. // id = getIntegerToken(content, &token[i + 1]);
  240. // i++;
  241. // }else if (jsoneq(content, &token[i], "port") == 0) {
  242. // port = getIntegerToken(content, &token[i + 1]);
  243. // i++;
  244. // }else if (jsoneq(content, &token[i], "ip") == 0) {
  245. // getStringToken(content, &token[i + 1], ip);
  246. // i++;
  247. // }else if (jsoneq(content, &token[i], "url") == 0) {
  248. // getStringToken(content, &token[i + 1], url);
  249. // i++;
  250. // }else if (jsoneq(content, &token[i], "name") == 0) {
  251. // getStringToken(content, &token[i + 1], name);
  252. // i++;
  253. // }
  254. // }
  255. start = 0;
  256. int idx = alarmExist(id);
  257. if(idx == -1){
  258. printf("New alarm found!\n");
  259. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  260. printf("Alarm id and name is: %d %s\n\n", id, name);
  261. // //zoek naar een vrije plaats in de alarm array
  262. // for(j = 0; j < maxAlarms(); j++){
  263. // if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  264. // setAlarm(time, name, ip, port, url, 5, id, j);
  265. // usedAlarms[j] = 1;
  266. // j = 10;
  267. // }
  268. // }
  269. // }else{
  270. // usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  271. }
  272. }
  273. // for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  274. // if(usedAlarms[j] == 0){
  275. // deleteAlarm(j);
  276. // };
  277. // }
  278. }
  279. bool NetworkIsReceiving(void){
  280. return isReceiving;
  281. }
  282. bool hasNetworkConnection(void){
  283. return hasNetwork;
  284. }