Tizen 2.0 Release
[framework/connectivity/bluez.git] / test / bnep-tx-test.c
1 #include <arpa/inet.h>
2 #include <netinet/in.h>
3 #include <net/if.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10
11 //#define SRV_IP "192.168.129.255"
12 #define SRV_IP "255.255.255.255"
13 #define PORT 9930
14
15 #define LEN     0x5a0
16
17 unsigned char buf[LEN];
18 int location=0;
19 void print_until(unsigned char until);
20 int make_data();
21 void diep(char *s);
22
23 void print_until(unsigned char until)
24 {
25         int i;
26         for ( i=0;i<=until;i++){
27                 //printf("%c",i);
28                 buf[location++]=i;
29         }
30 }
31
32 int make_data()
33 {
34         int i;
35
36         for (i=0;i<5;i++)
37                 print_until(0xff);
38
39         print_until(0x9f);
40
41
42         for (i=0;i<LEN;i++)
43                 printf("%c",buf[i]);
44
45         return 0;
46 }
47
48 /* diep(), #includes and #defines like in the server */
49 void diep(char *s)
50 {
51         perror(s);
52         exit(1);
53 }
54
55 int main(void)
56 {
57         struct sockaddr_in si_other;
58         struct ifreq interface;
59         int s, i, slen=sizeof(si_other);
60         int val=1,size=sizeof(val);
61         int retn;
62
63         printf("hello: Socket\n");
64         if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
65                 diep("socket");
66
67         printf("hello: sockopt\n");
68         memset(&interface, 0, sizeof(interface));
69         strncpy(interface.ifr_ifrn.ifrn_name, "bnep0",
70                         sizeof(interface.ifr_ifrn.ifrn_name));
71
72         if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, &interface,
73                                                 sizeof(interface)) < 0){
74                 diep("socket");
75                 close(s);
76         }
77         retn = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &val, size);
78         if (retn < 0) {
79                 perror("SO_BROADCAST");
80                 close(s);
81                 exit(-1);
82         }
83
84         make_data();
85
86         memset((char *) &si_other, 0, sizeof(si_other));
87         si_other.sin_family = AF_INET;
88         si_other.sin_port = htons(PORT);
89
90         if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
91                 fprintf(stderr, "inet_aton() failed\n");
92                 exit(1);
93         }
94
95         printf("hello: Send packet\n");
96         printf("Sending packet %d\n", i);
97         if (sendto(s, buf, LEN, 0, (const struct sockaddr *)&si_other, slen)==-1)
98                 diep("sendto()");
99
100         close(s);
101         return 0;
102 }
103