network.c 6.2 KB

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