network.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. bool isReceiving;
  26. bool hasNetwork;
  27. void NetworkInit() {
  28. hasNetwork = false;
  29. /* Register de internet controller. */
  30. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  31. printf("Registering failed. \n");
  32. }/* Netwerk configureren op dhcp */
  33. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  34. printf("DHCP failed. \n");
  35. }else {
  36. 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));
  37. }
  38. NutSleep(2000);
  39. hasNetwork = true;
  40. }
  41. char* getMacAdress(){
  42. ether_ntoa(confnet.cdn_mac);
  43. }
  44. char* httpGet(char address[]){
  45. isReceiving = true;
  46. printf("\n\n #-- HTTP get -- #\n");
  47. TCPSOCKET* sock = NutTcpCreateSocket();
  48. char buffer[2];
  49. char* content = (char*) calloc(1 , 800);
  50. char enters = 0;
  51. int t = 0;
  52. if(content == 0){
  53. printf("Can't calloc memory\n");
  54. }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  55. printf("Can't connect to server\n");
  56. }else{
  57. FILE *stream;
  58. stream = _fdopen((int) sock, "r+b");
  59. //Writing http GET to stream
  60. fprintf(stream, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  61. fflush(stream);
  62. printf("Headers writed. Now reading.");
  63. NutDelay(500);
  64. //Removing header:
  65. while(fgets(buffer, sizeof(buffer), stream) != NULL) {
  66. if(enters == 4) {
  67. content[t] = buffer[0];
  68. t++;
  69. }else {
  70. if (buffer[0] == '\n' || buffer[0] == '\r') {
  71. enters++;
  72. }
  73. else {
  74. enters = 0;
  75. }
  76. }
  77. }
  78. fclose(stream);
  79. }
  80. NutTcpCloseSocket(sock);
  81. content[t] = '\0';
  82. printf("\nContent size: %d, Content: %s \n", t, content);
  83. isReceiving = false;
  84. return content;
  85. }
  86. int getTimeZone()
  87. {
  88. char* content = httpGet("/gettimezone.php");
  89. int timezone = atoi(content);
  90. free(content);
  91. printf("%d", timezone);
  92. return timezone;
  93. }
  94. void parseAlarmJson(char* content){
  95. int r;
  96. int i;
  97. jsmn_parser p;
  98. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  99. jsmn_init(&p);
  100. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  101. if (r <= 0) {
  102. printf("Failed to parse JSON: %d \n", r);
  103. }else{
  104. printf("Aantal tokens found: %d \n", r);
  105. }
  106. int start = 1;
  107. int usedAlarms[maxAlarms()];
  108. int j;
  109. for(j = 0; j < maxAlarms(); j++){
  110. usedAlarms[j] = 0;
  111. }
  112. for(i = 1; i < r; i++)
  113. {
  114. struct _tm time = GetRTCTime();
  115. u_short port;
  116. char url[24];
  117. char ip[24];
  118. char name[16];
  119. memset(url, 0, 24);
  120. memset(ip, 0, 24);
  121. memset(name, 0, 16);
  122. int id;
  123. for (i = i; !((i + start) % 23 == 0); i++) {
  124. if (jsoneq(content, &token[i], "YYYY") == 0) {
  125. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  126. i++;
  127. }else if (jsoneq(content, &token[i], "MM") == 0) {
  128. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  129. i++;
  130. }else if (jsoneq(content, &token[i], "DD") == 0) {
  131. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  132. i++;
  133. }else if (jsoneq(content, &token[i], "hh") == 0) {
  134. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  135. i++;
  136. }else if (jsoneq(content, &token[i], "mm") == 0) {
  137. time.tm_min = getIntegerToken(content, &token[i + 1]);
  138. i++;
  139. }else if (jsoneq(content, &token[i], "ss") == 0) {
  140. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  141. i++;
  142. }else if (jsoneq(content, &token[i], "id") == 0) {
  143. id = getIntegerToken(content, &token[i + 1]);
  144. i++;
  145. }else if (jsoneq(content, &token[i], "port") == 0) {
  146. port = getIntegerToken(content, &token[i + 1]);
  147. i++;
  148. }else if (jsoneq(content, &token[i], "ip") == 0) {
  149. getStringToken(content, &token[i + 1], ip);
  150. i++;
  151. }else if (jsoneq(content, &token[i], "url") == 0) {
  152. getStringToken(content, &token[i + 1], url);
  153. i++;
  154. }else if (jsoneq(content, &token[i], "name") == 0) {
  155. getStringToken(content, &token[i + 1], name);
  156. i++;
  157. }
  158. }
  159. start = 0;
  160. int idx = alarmExist(id);
  161. if(idx == -1){
  162. printf("New alarm found!\n");
  163. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  164. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  165. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  166. printf("Alarm id and name is: %d %s\n\n", id, name);
  167. //zoek naar een vrije plaats in de alarm array
  168. for(j = 0; j < maxAlarms(); j++){
  169. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  170. setAlarm(time, name, ip, port, url, 5, id, j);
  171. usedAlarms[j] = 1;
  172. j = 10;
  173. }
  174. }
  175. }else{
  176. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  177. }
  178. }
  179. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  180. if(usedAlarms[j] == 0){
  181. deleteAlarm(j);
  182. };
  183. }
  184. }
  185. bool NetworkIsReceiving(void){
  186. return isReceiving;
  187. }
  188. bool hasNetworkConnection(void){
  189. return hasNetwork;
  190. }