network.c 6.4 KB

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