network.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "contentparser.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. void httpGet(char address[], void (*parser)(char*)){
  46. u_long rx_to = 10000;
  47. isReceiving = true;
  48. printf("\n\n #-- HTTP get -- #\n");
  49. TCPSOCKET* sock = NutTcpCreateSocket();
  50. char buffer[2];
  51. char* content = (char*) malloc(900);
  52. char enters = 0;
  53. int t = 0;
  54. if(content == 0){
  55. printf("Can't calloc memory\n");
  56. }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  57. printf("Can't connect to server\n");
  58. }else if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))){
  59. printf("Can't set sock options\n");
  60. }else{
  61. FILE *stream;
  62. stream = _fdopen((int) sock, "r+b");
  63. //Writing http GET to stream
  64. fprintf(stream, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  65. fflush(stream);
  66. printf("Headers writed. Now reading.");
  67. //Removing header:
  68. while(fgets(buffer, sizeof(buffer), stream) != NULL) {
  69. if(enters == 4) {
  70. content[t] = buffer[0];
  71. t++;
  72. }else {
  73. if (buffer[0] == '\n' || buffer[0] == '\r') {
  74. enters++;
  75. }
  76. else {
  77. enters = 0;
  78. }
  79. }
  80. }
  81. fclose(stream);
  82. content[t] = '\0';
  83. printf("\nContent size: %d, Content: %s \n", t, content);
  84. parser(content);
  85. free(content);
  86. }
  87. NutTcpCloseSocket(sock);
  88. isReceiving = false;
  89. }
  90. bool NetworkIsReceiving(void){
  91. return isReceiving;
  92. }
  93. bool hasNetworkConnection(void){
  94. return hasNetwork;
  95. }