Skip to content

Commit

Permalink
Update to latest. (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperl authored Dec 16, 2021
1 parent 5f59acf commit 281c670
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 38 deletions.
56 changes: 19 additions & 37 deletions lib/device.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the lib/LICENSE file.
import rpc
import uuid
import serialization show serialize deserialize
import drivers.cellular
import device_impl as impl
import uuid


/**
Functionality available on devices (ESP32).
*/

RPC_SYSTEM_DEVICE_NAME ::= 100
RPC_SYSTEM_DEVICE_HARDWARE_ID ::= 101
RPC_SYSTEM_FLASHSTORE_GET ::= 102
RPC_SYSTEM_FLASHSTORE_SET ::= 103
RPC_SYSTEM_FLASHSTORE_DELETE ::= 104
RPC_SYSTEM_CONSOLE_CONNECTION_OPEN ::= 105
RPC_SYSTEM_CONSOLE_CONNECTION_CLOSE ::= 106
RPC_SYSTEM_ESTIMATE_TIME_ACCURACY ::= 107
RPC_SYSTEM_GNSS_START ::= 108
RPC_SYSTEM_GNSS_LOCATION ::= 109
RPC_SYSTEM_GNSS_STOP ::= 110
interface Device_:
name -> string?
hardware_id -> uuid.Uuid
estimate_time_accuracy -> int?


/** Name of this device. */
name -> string?:
return rpc.invoke RPC_SYSTEM_DEVICE_NAME []
return impl.Device_.instance.name

/** Hardware ID of this device. */
hardware_id/uuid.Uuid ::= uuid.Uuid
rpc.invoke RPC_SYSTEM_DEVICE_HARDWARE_ID []
hardware_id/uuid.Uuid ::= impl.Device_.instance.hardware_id

/**
Estimated time accuracy of this device.
Expand All @@ -39,7 +32,7 @@ On the device, the time drifts. The time is corrigated using the NTP when the
accuracy when the time was last set plus the an estimated time drift.
*/
estimate_time_accuracy -> int?:
return rpc.invoke RPC_SYSTEM_ESTIMATE_TIME_ACCURACY []
return impl.Device_.instance.estimate_time_accuracy

/** Simple key-value store. */
interface Store:
Expand All @@ -63,61 +56,50 @@ class FlashStore implements Store:
Returns null if no value is available.
*/
get key/string -> any:
res := rpc.invoke RPC_SYSTEM_FLASHSTORE_GET [key]
if res is ByteArray:
return deserialize res
return null
return impl.FlashStore_.instance.get key

/**
Deletes the given $key from the store.
The $key does not need to be present in the store.
*/
delete key/string:
rpc.invoke RPC_SYSTEM_FLASHSTORE_DELETE [key]
impl.FlashStore_.instance.get key

/**
Inserts the given $key-$value pair in the store.
If the $key already exists in the store, then the value is overwritten.
*/
set key/string value/any:
rpc.invoke RPC_SYSTEM_FLASHSTORE_SET [key, serialize value]
impl.FlashStore_.instance.set key value

/**
Connection to the console.
Forces the kernel to attempt to connect to the console.
*/
class ConsoleConnection extends rpc.CloseableProxy:
class ConsoleConnection extends impl.ConsoleConnection_:
/** Opens a connection to the console. */
constructor.open:
super
rpc.invoke RPC_SYSTEM_CONSOLE_CONNECTION_OPEN []

close_rpc_selector_: return RPC_SYSTEM_CONSOLE_CONNECTION_CLOSE
super.open

/**
Control of the GNSS module controlled by the kernel.
*/
class Gnss extends rpc.CloseableProxy:
class Gnss extends impl.Gnss_:
/**
Starts GNSS.
The device must support GNSS.
*/
constructor.start:
super
rpc.invoke RPC_SYSTEM_GNSS_START []

close_rpc_selector_: return RPC_SYSTEM_GNSS_STOP
super.start

/**
The location of this device.
Returns null if there is no location fix.
*/
location -> cellular.GnssLocation?:
bytes := rpc.invoke RPC_SYSTEM_GNSS_LOCATION [handle_]
if not bytes: return null
return cellular.GnssLocation.deserialize bytes
return super
66 changes: 66 additions & 0 deletions lib/device_impl.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2021 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the lib/LICENSE file.
import drivers.cellular
import device
import uuid

/**
Implementation of ESP32 device library. Use the APIs in the device library.
*/

class Device_ implements device.Device_:
static instance_/Device_? := null
static hardware_id_/uuid.Uuid? := null

constructor.instance:
if not instance_: instance_ = Device_.init_

return instance_

constructor.init_:

name -> string?:
return get_mac_address_.stringify

hardware_id -> uuid.Uuid:
if not hardware_id_: hardware_id_ = uuid.uuid5 "hw_id" get_mac_address_
return hardware_id_

estimate_time_accuracy -> int?:
throw "NOT IMPLEMENTED"

class FlashStore_ implements device.Store:
static instance_/FlashStore_? := null

constructor.instance:
if not instance_: instance_ = FlashStore_.init_

return instance_

constructor.init_:

get key/string -> any:
throw "NOT IMPLEMENTED"

delete key/string:
throw "NOT IMPLEMENTED"

set key/string value/any:
throw "NOT IMPLEMENTED"

class ConsoleConnection_:
constructor.open:
throw "NOT IMPLEMENTED"

class Gnss_:

constructor.start:
throw "NOT IMPLEMENTED"

location -> cellular.GnssLocation?:
throw "NOT IMPLEMENTED"

get_mac_address_:
#primitive.esp32.get_mac_address
1 change: 1 addition & 0 deletions src/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ namespace toit {
PRIMITIVE(total_deep_sleep_time, 0) \
PRIMITIVE(total_run_time, 0) \
PRIMITIVE(image_config, 0) \
PRIMITIVE(get_mac_address, 0) \

#define MODULE_I2C(PRIMITIVE) \
PRIMITIVE(init, 3) \
Expand Down
12 changes: 12 additions & 0 deletions src/primitive_esp32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ PRIMITIVE(image_config) {
return result;
}

PRIMITIVE(get_mac_address) {
Error* error = null;
ByteArray* result = process->allocate_byte_array(6, &error);
if (result == null) return error;

ByteArray::Bytes bytes = ByteArray::Bytes(result);
esp_err_t err = esp_efuse_mac_get_default(bytes.address());
if (err != ESP_OK) memset(bytes.address(), 0, 6);

return result;
}

} // namespace toit

#endif // TOIT_FREERTOS
1 change: 0 additions & 1 deletion src/resources/wifi_esp32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ PRIMITIVE(get_rssi) {
return Smi::from(rssi);
}


} // namespace toit

#endif // TOIT_FREERTOS

0 comments on commit 281c670

Please sign in to comment.