Date:2015-06-11 10:01:12 (4 years 3 months ago)
Author:Stefan Schmidt
Commit:55de8c6373c79fb457b436a15e6b2901800a54e2
Message:atusb-eui64: add utility to read/write and EUI64 from/to the device EEPROM

Together with the new fw version (starting with version 0.3) it is now possible
to set a permament EUI64 address into the devices EEPROM. The Linux driver will
get updated to choose this address, if available, instead of using a randomly
generated address.
Files: tools/Makefile (1 diff)
tools/atusb-eui64/Makefile (1 diff)
tools/atusb-eui64/atusb-eui64.c (1 diff)

Change Details

tools/Makefile
1212
1313
1414BEN_DIRS=atrf-gpio atrf-id atrf-path atrf-proxy atrf-reset \
15     atrf-rssi atrf-trim atrf-txrx atrf-xmit atrf-xtal
15     atrf-rssi atrf-trim atrf-txrx atrf-xmit atrf-xtal \
16     atusb-eui64
1617
1718ifneq ($(wildcard ../install/lowpan-tools-0.2.2/include/ieee802154.h),)
1819DIRTPAN := dirtpan
tools/atusb-eui64/Makefile
1#
2# atusb-eui64/Makefile - Build the ATUSB EUI64 EEPROM writing and readin utility
3#
4# Written 2015 by Stefan Schmidt
5# Copyright 2015 Stefan Schmidt
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12
13
14MAIN = atusb-eui64
15
16include ../Makefile.common
tools/atusb-eui64/atusb-eui64.c
1#ifdef HAVE_USB
2/*
3 * atusb-eui64/atusb-eui64.c - Read and write EUI64 address from/to EEPROM
4 *
5 * Written 2015, 2016 by Stefan Schmidt
6 * Copyright 2015, 2016 Stefan Schmidt
7 *
8 * Based on atrf-id with following copyright:
9 * Written 2010-2011, 2013 by Werner Almesberger
10 * Copyright 2010-2011, 2013 Werner Almesberger
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22
23#include <usb.h>
24
25#include "atusb/ep0.h"
26#include "atrf.h"
27
28#define FROM_DEV ATUSB_FROM_DEV(0)
29#define TO_DEV ATUSB_TO_DEV(0)
30
31#define EUI64_LEN 8
32#define BUF_SIZE 256
33
34static int get_eui64(usb_dev_handle *dev, void *data, int size)
35{
36    int res;
37
38    res = usb_control_msg(dev, FROM_DEV, ATUSB_EUI64_READ, 0, 0, data,
39                  size, 1000);
40    if (res < 0)
41        fprintf(stderr, "ATUSB_EUI64_READ: %s\n", usb_strerror());
42    return res;
43}
44
45static int set_eui64(usb_dev_handle *dev, void *data, int size)
46{
47    int res;
48
49    res = usb_control_msg(dev, TO_DEV, ATUSB_EUI64_WRITE, 0, 0, data, size,
50                  1000);
51    if (res < 0)
52        fprintf(stderr, "ATUSB_EUI64_WRITE: %s\n", usb_strerror());
53    return res;
54}
55
56static int get_id(usb_dev_handle *dev, void *data, int size)
57{
58    int res;
59
60    res = usb_control_msg(dev, FROM_DEV, ATUSB_ID, 0, 0, data, size, 1000);
61    if (res < 0)
62        fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
63    return res;
64}
65
66static int get_protocol(usb_dev_handle *dev, uint8_t *major, uint8_t *minor,
67            uint8_t *target)
68{
69    uint8_t ids[3];
70
71    if (get_id(dev, ids, 3) < 0)
72        return -1;
73    if (major)
74        *major = ids[0];
75    if (minor)
76        *minor = ids[1];
77    if (target)
78        *target = ids[2];
79
80    return 0;
81}
82
83static int get_build(usb_dev_handle *dev, char *buf, size_t size)
84{
85    int res;
86
87    res = usb_control_msg(dev, FROM_DEV, ATUSB_BUILD, 0, 0, buf, size,
88        1000);
89    if (res < 0)
90        fprintf(stderr, "ATUSB_BUILD: %s\n", usb_strerror());
91    return res;
92}
93
94static void show_usb_info(struct atrf_dsc *dsc, unsigned char *mac, int write)
95{
96    usb_dev_handle *dev;
97    const struct usb_device *device;
98    uint8_t major, minor, target;
99    const char *mcu;
100    char buf[BUF_SIZE+1]; /* +1 for terminating \0 */
101    char serial[BUF_SIZE];
102    int len, i;
103    uint8_t eui64[EUI64_LEN];
104
105    dev = atrf_usb_handle(dsc);
106    if (!dev)
107        return;
108    device = usb_device(dev);
109
110    printf("Found ATUSB device (%04x:%04x) ",
111        device->descriptor.idVendor, device->descriptor.idProduct);
112
113    if (device->descriptor.iSerialNumber > 0)
114        usb_get_string_simple(dev, device->descriptor.iSerialNumber,
115                      serial, BUF_SIZE);
116
117    printf("with serial number %s\n", serial);
118
119    if (get_protocol(dev, &major, &minor, &target) < 0)
120        exit(1);
121    switch (target) {
122    case HW_TYPE_100813:
123    case HW_TYPE_101216:
124        mcu = "C8051F326";
125        break;
126    case HW_TYPE_110131:
127        mcu = "ATmega32U2";
128        break;
129    default:
130        mcu = "???";
131    }
132    printf("Firmware version %u.%u on hw %u (%s) ", major, minor, target,
133           mcu);
134
135    len = get_build(dev, buf, sizeof(buf)-1);
136    if (len < 0)
137        exit(1);
138    buf[len] = 0;
139    printf("build: %s\n", buf);
140
141    if (target != HW_TYPE_110131) {
142        printf("Firmware not able to set EUI64 on %s\n", mcu);
143        exit(1);
144    }
145
146    if (major == 0 && minor < 3) {
147        printf("Firmware to old. You need at least version 0.3\n");
148        exit(1);
149    }
150
151    /* No new EUI64 is given, just read out */
152    if (!write) {
153        get_eui64(dev, eui64, EUI64_LEN);
154        printf("Current EUI64 address from EEPROM: ");
155        for (i = 0; i < EUI64_LEN; i++) {
156            printf("%02x", eui64[EUI64_LEN - 1 - i]);
157            if (i < EUI64_LEN -1)
158                printf(":");
159        }
160        printf("\n");
161    }
162    /* We got a new EUI64 as argument, write it */
163    if (write) {
164        printf("Writing EUI64 address ");
165        for (i = 0; i < EUI64_LEN; i++) {
166            printf("%02x", mac[EUI64_LEN - 1 - i]);
167            if (i < EUI64_LEN -1)
168                printf(":");
169        }
170        printf(" to EEPROM\n");
171        set_eui64(dev, mac, EUI64_LEN);
172    }
173}
174
175/* Taken from wpan-tools/src/interface.c which is BSD licensed and written
176 * by Alexander Aring
177 */
178static int extendedaddr_a2n(unsigned char *mac_addr, char *arg)
179{
180    int i;
181
182    for (i = 0; i < EUI64_LEN ; i++) {
183        int temp;
184        char *cp = strchr(arg, ':');
185        if (cp) {
186            *cp = 0;
187            cp++;
188        }
189        if (sscanf(arg, "%x", &temp) != 1)
190            return -1;
191        if (temp < 0 || temp > 255)
192            return -1;
193
194        mac_addr[EUI64_LEN - 1 - i] = temp;
195        if (!cp)
196            break;
197        arg = cp;
198    }
199    if (i < EUI64_LEN - 1)
200        return -3;
201
202    return 0;
203}
204
205static void usage(const char *name)
206{
207    fprintf(stderr, "usage: %s [-a 00:11:22:33:44:55:66:77]\n", name);
208    exit(1);
209}
210
211int main(int argc, char *const *argv)
212{
213    uint8_t mac[EUI64_LEN];
214    struct atrf_dsc *dsc;
215    int c;
216    int write = 0;
217
218    while ((c = getopt(argc, argv, "a:")) != EOF)
219        switch (c) {
220        case 'a':
221            extendedaddr_a2n(mac, optarg);
222            write = 1;
223            break;
224        default:
225            usage(*argv);
226        }
227
228    dsc = atrf_open(NULL);
229    if (!dsc)
230        return 1;
231
232    show_usb_info(dsc, mac, write);
233
234    atrf_close(dsc);
235
236    return 0;
237}
238#endif /* HAVE_USB */

Archive Download the corresponding diff file



interactive