From 431c5075835c958355aa1f133003401d53d4e582 Mon Sep 17 00:00:00 2001 From: David Antliff Date: Fri, 4 Aug 2017 19:01:56 +1200 Subject: [PATCH] Add owb_verify_rom(). Add MIT license to all source files. --- LICENSE | 21 +++++++++++ main/ds18b20.c | 24 ++++++++++++ main/ds18b20.h | 24 ++++++++++++ main/ds18b20_main.c | 41 +++++++++++++++++---- main/ds18b20_static.h | 24 ++++++++++++ main/owb.c | 68 ++++++++++++++++++++++++++++++---- main/owb.h | 85 ++++++++++++++++++++++++++++++++++++++----- main/owb_static.h | 24 ++++++++++++ 8 files changed, 288 insertions(+), 23 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..441ee32 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 David Antliff + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/main/ds18b20.c b/main/ds18b20.c index 79637aa..7c0471c 100644 --- a/main/ds18b20.c +++ b/main/ds18b20.c @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #include #include #include diff --git a/main/ds18b20.h b/main/ds18b20.h index 75d488e..c805260 100644 --- a/main/ds18b20.h +++ b/main/ds18b20.h @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #ifndef DS18B20_H #define DS18B20_H diff --git a/main/ds18b20_main.c b/main/ds18b20_main.c index 3db8ab1..27a2093 100644 --- a/main/ds18b20_main.c +++ b/main/ds18b20_main.c @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #include #include "freertos/FreeRTOS.h" @@ -23,7 +47,7 @@ void app_main() { - esp_log_level_set("*", ESP_LOG_INFO); // set all components to INFO level + esp_log_level_set("*", ESP_LOG_INFO); // Create a 1-Wire bus #ifdef USE_STATIC @@ -44,17 +68,20 @@ void app_main() bool found = owb_search_first(owb, &search_state); while (found) { - printf(" %d : ", num_devices); - for (int i = 7; i >= 0; i--) - { - printf("%02x", ((uint8_t *)(&search_state.rom_code))[i]); - } - printf("\n"); + char rom_code_s[17]; + owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s)); + printf(" %d : %s\n", num_devices, rom_code_s); device_rom_codes[num_devices] = search_state.rom_code; ++num_devices; found = owb_search_next(owb, &search_state); } + // known ROM codes (LSB first): + OneWireBus_ROMCode known_device = { { {0x28}, {0xee, 0xcc, 0x87, 0x2e, 0x16, 0x01}, {0x00} } }; + char rom_code_s[17]; + owb_string_from_rom_code(known_device, rom_code_s, sizeof(rom_code_s)); + printf("Device %s is %s\n", rom_code_s, owb_verify_rom(owb, known_device) ? "present" : "not present"); + //uint64_t rom_code = 0x0001162e87ccee28; // pink //uint64_t rom_code = 0xf402162c6149ee28; // green //uint64_t rom_code = 0x1502162ca5b2ee28; // orange diff --git a/main/ds18b20_static.h b/main/ds18b20_static.h index ed7d6e3..238dcb3 100644 --- a/main/ds18b20_static.h +++ b/main/ds18b20_static.h @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + // Expose structure for stack-based (static) allocation. #ifndef DS18B20_STATIC_H diff --git a/main/owb.c b/main/owb.c index a627ca1..5211e73 100644 --- a/main/owb.c +++ b/main/owb.c @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #include #include #include @@ -421,12 +445,6 @@ void owb_use_crc(OneWireBus * bus, bool use_crc) } } -int owb_rom_search(OneWireBus * bus) -{ - // TODO - return 0; -} - OneWireBus_ROMCode owb_read_rom(const OneWireBus * bus) { OneWireBus_ROMCode rom_code = {0}; @@ -445,7 +463,9 @@ OneWireBus_ROMCode owb_read_rom(const OneWireBus * bus) memset(rom_code.bytes, 0, sizeof(rom_code)); } } - ESP_LOGD(TAG, "rom_code %08" PRIx64, rom_code); + char rom_code_s[17]; + owb_string_from_rom_code(rom_code, rom_code_s, sizeof(rom_code_s)); + ESP_LOGD(TAG, "rom_code %s", rom_code_s); } else { @@ -455,6 +475,30 @@ OneWireBus_ROMCode owb_read_rom(const OneWireBus * bus) return rom_code; } +bool owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code) +{ + bool result = false; + if (_is_init(bus)) + { + OneWireBus_SearchState state = { + .last_discrepancy = 64, + .last_device_flag = false, + }; + + if (_search(bus, &state)) + { + result = true; + for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i) + { + result = rom_code.bytes[i] == state.rom_code.bytes[i]; + ESP_LOGD(TAG, "%02x %02x", rom_code.bytes[i], state.rom_code.bytes[i]); + } + ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not "); + } + } + return result; +} + bool owb_reset(const OneWireBus * bus) { return _reset(bus); @@ -526,3 +570,13 @@ bool owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state) } return result; } + +char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len) +{ + for (int i = sizeof(rom_code.bytes) - 1; i >= 0; i--) + { + sprintf(buffer, "%02x", rom_code.bytes[i]); + buffer += 2; + } + return buffer; +} diff --git a/main/owb.h b/main/owb.h index c1306f5..c941c10 100644 --- a/main/owb.h +++ b/main/owb.h @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #ifndef ONE_WIRE_BUS_H #define ONE_WIRE_BUS_H @@ -18,6 +42,11 @@ extern "C" { typedef struct _OneWireBus OneWireBus; +/** + * @brief Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where + * the first byte is the family number, then the following 6 bytes form the + * serial number. The final byte is the CRC8 check byte. + */ typedef union { struct fields @@ -66,7 +95,12 @@ void owb_use_crc(OneWireBus * bus, bool use_crc); */ OneWireBus_ROMCode owb_read_rom(const OneWireBus * bus); -// TODO +/** + * @brief Verify the device specified by ROM code is present. + * @param[in] bus Pointer to initialised bus instance. + * @param[in] rom_code ROM code to verify. + * @return true if device is present, false if not present. + */ bool owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code); /** @@ -120,30 +154,63 @@ void owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code); * @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. * @param[in] data Byte to feed into CRC. * @return Resultant CRC value. + * Should be zero if last byte was the CRC byte and the CRC matches. */ uint8_t owb_crc8_byte(uint8_t crc, uint8_t data); - -// TODO +/** + * @brief 1-Wire 8-bit CRC lookup with accumulation over a block of bytes. + * @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. + * @param[in] data Array of bytes to feed into CRC. + * @param[in] len Length of data array in bytes. + * @return Resultant CRC value. + * Should be zero if last byte was the CRC byte and the CRC matches. + */ uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len); // Search API -struct OneWireBus_SearchState + +/** + * @brief Represents the state of a device search on the 1-Wire bus. Pass a pointer to + * this structure to `owb_search_first` and `owb_search_next` to iterate through + * detected devices on the bus. + */ +typedef struct { OneWireBus_ROMCode rom_code; int last_discrepancy; int last_family_discrepancy; int last_device_flag; -}; -typedef struct OneWireBus_SearchState OneWireBus_SearchState; +} OneWireBus_SearchState; +/** + * @brief Locates the first device on the 1-Wire bus, if present. + * @param[in] bus Pointer to initialised bus instance. + * @param[in,out] state Pointer to an existing search state structure. + * @return True if a device is found, false if no devices are found. + * If a device is found, the ROM Code can be obtained from the state. + */ bool owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state); + +/** + * @brief Locates the next device on the 1-Wire bus, if present, starting from + * the provided state. Further calls will yield additional devices, if present. + * @param[in] bus Pointer to initialised bus instance. + * @param[in,out] state Pointer to an existing search state structure. + * @return True if a device is found, false if no devices are found. + * If a device is found, the ROM Code can be obtained from the state. + */ bool owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state); -// TODO: maybe not useful? -uint64_t uint64_from_rom_code(OneWireBus_ROMCode rom_code); -OneWireBus_ROMCode rom_code_from_uint64(uint64_t rom_code); +/** + * @brief Create a string representation of a ROM code. + * @param[in] rom_code The ROM code to convert to string representation. + * @param[out] buffer The destination for the string representation. It will be null terminated. + * @param[in] len The length of the buffer in bytes. 64-bit ROM codes require 16 characters + * to represent as a string, plus a null terminator, for 17 bytes. + */ +char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len); #ifdef __cplusplus diff --git a/main/owb_static.h b/main/owb_static.h index 7d8fd67..e90575a 100644 --- a/main/owb_static.h +++ b/main/owb_static.h @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2017 David Antliff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + // Expose structure for stack-based (static) allocation. #ifndef ONE_WIRE_BUS_STATIC_H