network.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <net/route.h>
  13. #include <dev/nicrtl.h>
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <arpa/inet.h>
  17. #include <pro/dhcp.h>
  18. #include <pro/sntp.h>
  19. #include "network.h"
  20. #include "ntp.h"
  21. #include "jsmn.h"
  22. void NetworkInit() {
  23. /* Register de internet controller. */
  24. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  25. printf("Registering failed.");
  26. }/* Netwerk configureren op dhcp */
  27. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  28. /* Done. */
  29. }else {
  30. printf("Ik heb een internet connectie. Ip is: %s", inet_ntoa(confnet.cdn_ip_addr));
  31. }
  32. }
  33. char* httpGet(char address[]){
  34. printf("komt in httpget()");
  35. NutDelay(1000);
  36. TCPSOCKET* sock = NutTcpCreateSocket();
  37. char http[150];
  38. sprintf(http, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  39. printf("%s", http);
  40. NutDelay(100);
  41. char buffer[250];
  42. int len = sizeof(http);
  43. if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  44. printf("Can't connect to server\n");
  45. }else{
  46. FILE *stream;
  47. stream = _fdopen((int) sock, "r b");
  48. if(NutTcpSend(sock, http, len) != len){
  49. printf("Writing headers failed.");
  50. NutDelay(1000);
  51. }else{
  52. printf("Headers writed. Now reading.");
  53. NutDelay(1000);
  54. NutTcpReceive(sock, buffer, sizeof(buffer));
  55. //fread(buffer, 1, sizeof(buffer), stream);
  56. NutDelay(1000);
  57. printf(buffer);
  58. };
  59. fclose(stream);
  60. }
  61. NutTcpCloseSocket(sock);
  62. int i;
  63. int enters = 0;
  64. int t = 0;
  65. char* content = (char*) calloc(1 , sizeof(buffer));
  66. for(i = 0; i < strlen(buffer); i++)
  67. {
  68. if(enters > 3) {
  69. enters = 10;
  70. content[t] = buffer[i];
  71. t++;
  72. }
  73. if(buffer[i] == '\n' || buffer[i] == '\r')
  74. {
  75. enters++;
  76. }
  77. else if(enters < 10)
  78. {
  79. enters = 0;
  80. }
  81. }
  82. content[t] = '\0';
  83. printf("\n Content size %d\n", t);
  84. printf("\n Content: %s", content);
  85. return content;
  86. }
  87. static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
  88. if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
  89. strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
  90. return 0;
  91. }
  92. return -1;
  93. }
  94. void parseJson(char* content){
  95. int r;
  96. int i;
  97. jsmn_parser p;
  98. jsmntok_t token[50]; /* 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. for (i = 1; i < r; i++) {
  107. printf("\n Json token: Start: %d, End: %d", token[i].start, token[i].end);
  108. if (jsoneq(content, &token[i], "tijd") == 0) {
  109. /* We may use strndup() to fetch string value */
  110. printf("- Tijd: %.*s\n", token[i+1].end-token[i+1].start,
  111. content + token[i+1].start);
  112. i++;
  113. }
  114. }
  115. }