note
[oweals/gnunet.git] / src / transport / wlan / helper_common.c
1 /*
2  * helper_common.c
3  *
4  *  Created on: 28.03.2011
5  *      Author: David Brodski
6  */
7
8 #include <sys/socket.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <resolv.h>
14 #include <string.h>
15 #include <utime.h>
16 #include <unistd.h>
17 #include <getopt.h>
18
19 #include "platform.h"
20 #include "gnunet_constants.h"
21 #include "gnunet_os_lib.h"
22 #include "gnunet_transport_plugin.h"
23 #include "transport.h"
24 #include "gnunet_util_lib.h"
25 #include "plugin_transport_wlan.h"
26 #include "gnunet_common.h"
27 #include "gnunet_transport_plugin.h"
28 //#include "gnunet_util_lib.h"
29
30 /**
31  * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin
32  * @param buffer pointer to buffer for the message
33  * @param mac pointer to the mac address
34  * @return number of bytes written
35  */
36
37 int
38 send_mac_to_plugin(char * buffer, uint8_t * mac)
39 {
40
41   struct Wlan_Helper_Control_Message macmsg;
42
43   memcpy(macmsg.mac.mac, mac, sizeof(struct MacAddress));
44   macmsg.hdr.size = htons(sizeof(struct Wlan_Helper_Control_Message));
45   macmsg.hdr.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
46
47   memcpy(buffer, &macmsg, sizeof(struct Wlan_Helper_Control_Message));
48   return sizeof(struct Wlan_Helper_Control_Message);
49 }
50
51 /*
52    *  Copyright (c) 2008, Thomas d'Otreppe
53    *
54    *  Common OSdep stuff
55    *
56    *  This program is free software; you can redistribute it and/or modify
57    *  it under the terms of the GNU General Public License as published by
58    *  the Free Software Foundation; either version 2 of the License, or
59    *  (at your option) any later version.
60    *
61    *  This program is distributed in the hope that it will be useful,
62    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
63    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64    *  GNU General Public License for more details.
65    *
66    *  You should have received a copy of the GNU General Public License
67    *  along with this program; if not, write to the Free Software
68    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
69    */
70
71 /**
72  * Return the frequency in Mhz from a channel number
73  */
74 int getFrequencyFromChannel(int channel)
75 {
76         static int frequencies[] = {
77                 -1, // No channel 0
78                 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
79                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Nothing from channel 15 to 34 (exclusive)
80                 5170, 5175, 5180, 5185, 5190, 5195, 5200, 5205, 5210, 5215, 5220, 5225, 5230, 5235, 5240, 5245,
81                 5250, 5255, 5260, 5265, 5270, 5275, 5280, 5285, 5290, 5295, 5300, 5305, 5310, 5315, 5320, 5325,
82                 5330, 5335, 5340, 5345, 5350, 5355, 5360, 5365, 5370, 5375, 5380, 5385, 5390, 5395, 5400, 5405,
83                 5410, 5415, 5420, 5425, 5430, 5435, 5440, 5445, 5450, 5455, 5460, 5465, 5470, 5475, 5480, 5485,
84                 5490, 5495, 5500, 5505, 5510, 5515, 5520, 5525, 5530, 5535, 5540, 5545, 5550, 5555, 5560, 5565,
85                 5570, 5575, 5580, 5585, 5590, 5595, 5600, 5605, 5610, 5615, 5620, 5625, 5630, 5635, 5640, 5645,
86                 5650, 5655, 5660, 5665, 5670, 5675, 5680, 5685, 5690, 5695, 5700, 5705, 5710, 5715, 5720, 5725,
87                 5730, 5735, 5740, 5745, 5750, 5755, 5760, 5765, 5770, 5775, 5780, 5785, 5790, 5795, 5800, 5805,
88                 5810, 5815, 5820, 5825, 5830, 5835, 5840, 5845, 5850, 5855, 5860, 5865, 5870, 5875, 5880, 5885,
89                 5890, 5895, 5900, 5905, 5910, 5915, 5920, 5925, 5930, 5935, 5940, 5945, 5950, 5955, 5960, 5965,
90                 5970, 5975, 5980, 5985, 5990, 5995, 6000, 6005, 6010, 6015, 6020, 6025, 6030, 6035, 6040, 6045,
91                 6050, 6055, 6060, 6065, 6070, 6075, 6080, 6085, 6090, 6095, 6100
92         };
93
94         return (channel > 0 && channel <= 221) ? frequencies[channel] : -1;
95 }
96
97 /**
98  * Return the channel from the frequency (in Mhz)
99  */
100 int getChannelFromFrequency(int frequency)
101 {
102         if (frequency >= 2412 && frequency <= 2472)
103                 return (frequency - 2407) / 5;
104         else if (frequency == 2484)
105                 return 14;
106         else if (frequency >= 5000 && frequency <= 6100)
107                 return (frequency - 5000) / 5;
108         else
109                 return -1;
110 }