Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9409910
  • 博文数量: 1748
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 20070
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1748)

文章存档

2024年(24)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2015-05-27 11:07:31


点击(此处)折叠或打开

  1. #if 1 //multicast -> emulate easylink of mico .
  2. #define DEMO_AP_SSID        "xiaomi"
  3. #define DEMO_AP_PWD        "xiaomi99"

  4. #define EASYLINK_PORT        10000
  5. #define EASYLINK_SYNC        "abcdefghijklmnopqrstuvw"
  6. #define EASYLINK_EXT_NO_USERINFO        "ND,0,121.207.243.132,8086,111111,C,222222"

  7. static void broadcast_client_task_exec(os_thread_arg_t arg)
  8. {
  9.     int iret, addr_len, yes;
  10.     unsigned char one;
  11.     struct sockaddr_in LocalAddr, to;    
  12.     struct ip_mreq mc;
  13.             
  14.     info("multicast of mico startting...");

  15.     uint32_t mcast_addr = inet_addr("224.0.0.251");
  16.     uint8_t mcast_mac[6];

  17.     wifi_get_ipv4_multicast_mac(ntohl(mcast_addr), mcast_mac);
  18.     wifi_add_mcast_filter(mcast_mac);

  19.     info("multicast mac is ");
  20.     dump_hex(mcast_mac, 6);

  21.     int sock_udp = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  22.     if (sock_udp >= 0) {    
  23.         yes = 1;
  24.         setsockopt(sock_udp, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes));
  25.         one = 0;
  26.         setsockopt(sock_udp, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&one, sizeof(one));    
  27.         one = 255;
  28.         setsockopt(sock_udp, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&one, sizeof(one));    
  29.         
  30.         mc.imr_multiaddr.s_addr = mcast_addr;
  31.         mc.imr_interface.s_addr = htonl(INADDR_ANY);
  32.         if (setsockopt(sock_udp, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mc, sizeof(mc)) < 0) {            
  33.             info("multicast join-group failed.");
  34.             //fixme: error for bind.
  35.             close(sock_udp);
  36.             return;
  37.         }
  38.         
  39.         memset(&LocalAddr, 0, sizeof(LocalAddr));
  40.         
  41.         LocalAddr.sin_family = AF_INET;
  42.         LocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  43.         LocalAddr.sin_port = htons(EASYLINK_PORT);
  44.         addr_len = sizeof(struct sockaddr_in);

  45.         iret = bind(sock_udp, (struct sockaddr *)&LocalAddr, addr_len);
  46.         if (iret < 0) {            
  47.             info("multicast monitor binding failed.");
  48.             //fixme: error for bind.
  49.             close(sock_udp);
  50.             return;
  51.         }

  52.         // 组织测试数据。
  53.         //len_ssid, len_key, ssid, len, len_userdata, flag, data.
  54.         char msg[256], msg_sync[256], ip_addr[32], msg_tmp[256];
  55.         unsigned char msg_len = 0, iTmp, iExt;

  56.         memset(msg_tmp, 0, sizeof(msg_tmp));

  57.         strcpy((char *)msg_sync, EASYLINK_SYNC);
  58.         msg[msg_len++] = strlen(DEMO_AP_SSID);
  59.         msg[msg_len++] = strlen(DEMO_AP_PWD);            
  60.         strcpy(&msg[msg_len], DEMO_AP_SSID);
  61.         msg_len += strlen(DEMO_AP_SSID);
  62.         strcpy(&msg[msg_len], DEMO_AP_PWD);
  63.         msg_len += strlen(DEMO_AP_PWD);

  64.         if ((msg_len%2)>0)
  65.             msg[msg_len++] = 0; //补足偶数位
  66.         //*
  67.         msg[msg_len++] = strlen(EASYLINK_EXT_NO_USERINFO);
  68.         msg[msg_len++] = 0;
  69.         strcpy(&msg[msg_len], EASYLINK_EXT_NO_USERINFO);
  70.         msg_len += strlen(EASYLINK_EXT_NO_USERINFO);
  71.         
  72.         if ((msg_len%2)>0)
  73.             msg[msg_len++] = 0; //补足偶数位
  74.         //*/
  75.         info("multicast msg is ");
  76.         dump_hex(msg, msg_len);    

  77.         //发送数据
  78.         while (1) {
  79.             //向 239.118.0.0 发送同步头 5 次。 EASYLINK_SYNC
  80.             to.sin_family = AF_INET;
  81.             to.sin_port = htons(EASYLINK_PORT);
  82.             to.sin_addr.s_addr = inet_addr("239.118.0.0");
  83.     
  84.             for (iTmp = 0; iTmp < 5; iTmp ++) {
  85.                 while ((iret = sendto(sock_udp, (char *)msg_sync, 20 /*strlen(EASYLINK_SYNC)*/,
  86.                     0, (struct sockaddr *)&to, sizeof(struct sockaddr_in))) != 20);
  87.                 ;
  88.                 info("send sync: %d bytes ", iret);
  89.                 os_thread_sleep(os_msec_to_ticks(50));
  90.             }

  91.             //发送组播报文。
  92.             for (iTmp=0; iTmp<msg_len; iTmp+=2) {
  93.                 memset(ip_addr, 0, sizeof(ip_addr));
  94.                 sprintf(ip_addr, "239.126.%d.%d", (int)(msg[iTmp] & 0xff), (int)(msg[iTmp+1] & 0xff));                
  95.                 to.sin_addr.s_addr = inet_addr(ip_addr);

  96.                 iExt = iTmp/2 + 20;
  97.                 while ((iret = sendto(sock_udp, (char *)msg_tmp, iExt,
  98.                     0, (struct sockaddr *)&to, sizeof(struct sockaddr_in))) != iExt); //发送随机内容到组。
  99.                 info("sendto [%s]: %d bytes ", ip_addr, iret);                
  100.                 
  101.                 //os_thread_sleep(os_msec_to_ticks(50));
  102.             }

  103.             os_thread_sleep(os_msec_to_ticks(200));            
  104.         }
  105.         
  106.     }
  107.     //while (1) os_thread_sleep(os_msec_to_ticks(3000));
  108.     os_thread_self_complete(NULL);    
  109. }
  110. #else

阅读(4310) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~