palm-client: beam a contact (More > Beam), yourself (Contacts > Beam My Contact) or a channel (Channel > Beam Channel) to another VisorMesh over IR, as the meshcore:// links MeshCore One shares as QR codes. A beam is received in segment 1 without globals and saved to an inbox; the app then asks to add the contact or join the channel (now, later or discard). ChannelJoin, from Add Channel, joins or opens a channel by key
This commit is contained in:
1 parent
7f982b872b
commit
f3be31bd76
5 files changed
+329
-25
No files matched your search
@@ -0,0 +1,181 @@
|
||||
#include <PalmOS.h>
|
||||
#include "resources.h"
|
||||
#include "app.h"
|
||||
#include "session.h"
|
||||
#include "contacts.h"
|
||||
#include "channels.h"
|
||||
#include "channel_form.h"
|
||||
#include "protocol2.h"
|
||||
#include "ui/text.h"
|
||||
#include "beam.h"
|
||||
|
||||
static void Send(const Char *link, UInt16 len, const Char *what) VM_SECT5;
|
||||
static Boolean Handle(const Char *link, UInt16 len, Boolean *keep) VM_SECT5;
|
||||
|
||||
static void Send(const Char *link, UInt16 len, const Char *what)
|
||||
{
|
||||
ExgSocketType s;
|
||||
Char name[VM_NAME_SIZE + 8];
|
||||
Err err;
|
||||
|
||||
if (len == 0) {
|
||||
FrmCustomAlert(SettingsErrorAlert, "That name is too long to beam.", "", "");
|
||||
return;
|
||||
}
|
||||
StrPrintF(name, "VisorMesh.%s", VM_BEAM_EXT);
|
||||
MemSet(&s, sizeof(s), 0);
|
||||
s.target = VM_CREATOR;
|
||||
s.description = (Char *)what;
|
||||
s.name = name;
|
||||
s.length = len;
|
||||
err = ExgPut(&s);
|
||||
if (err == errNone)
|
||||
ExgSend(&s, link, len, &err);
|
||||
ExgDisconnect(&s, err);
|
||||
}
|
||||
|
||||
void BeamContact(const VmContact *c)
|
||||
{
|
||||
Char link[VM_BEAM_MAX];
|
||||
UInt8 utf8[VM_NAME_SIZE];
|
||||
UInt16 n = TxtPalmToUtf8(c->name, StrLen(c->name), utf8, sizeof(utf8));
|
||||
|
||||
Send(link, n ? VmShareContact(link, sizeof(link), utf8, n, c->pubKey, c->type) : 0, c->name);
|
||||
}
|
||||
|
||||
void BeamSelf(void)
|
||||
{
|
||||
const VmSelfInfo *self = SessionSelf();
|
||||
Char link[VM_BEAM_MAX];
|
||||
UInt8 utf8[VM_NAME_SIZE];
|
||||
UInt16 n;
|
||||
|
||||
if (!SessionIsConnected()) {
|
||||
FrmCustomAlert(SettingsErrorAlert, "Connect to the radio first: your key comes from it.", "", "");
|
||||
return;
|
||||
}
|
||||
n = TxtPalmToUtf8(self->name, StrLen(self->name), utf8, sizeof(utf8));
|
||||
Send(link, n ? VmShareContact(link, sizeof(link), utf8, n, self->pubKey, self->advType ? self->advType : VM_ADV_TYPE_CHAT) : 0,
|
||||
self->name);
|
||||
}
|
||||
|
||||
void BeamChannel(UInt8 slot)
|
||||
{
|
||||
VmChannel c;
|
||||
Char link[VM_BEAM_MAX];
|
||||
UInt8 utf8[VM_NAME_SIZE];
|
||||
UInt16 n;
|
||||
|
||||
if (!ChannelsFindSlot(slot, &c)) {
|
||||
FrmCustomAlert(SettingsErrorAlert, "Open the channel you want to beam first.", "", "");
|
||||
return;
|
||||
}
|
||||
n = TxtPalmToUtf8(c.name, StrLen(c.name), utf8, sizeof(utf8));
|
||||
Send(link, n ? VmShareChannel(link, sizeof(link), utf8, n, c.secret) : 0, c.name);
|
||||
}
|
||||
|
||||
void BeamInboxNote(void)
|
||||
{
|
||||
LocalID id = DmFindDatabase(0, VM_INBOX_NAME);
|
||||
DmOpenRef db;
|
||||
|
||||
if (!id)
|
||||
return;
|
||||
db = DmOpenDatabase(0, id, dmModeReadOnly);
|
||||
if (!db)
|
||||
return;
|
||||
if (DmNumRecords(db) > 0)
|
||||
FtrSet(VM_CREATOR, VM_FTR_INBOX, 1);
|
||||
DmCloseDatabase(db);
|
||||
}
|
||||
|
||||
Boolean BeamInboxWaiting(void)
|
||||
{
|
||||
UInt32 v;
|
||||
|
||||
return FtrGet(VM_CREATOR, VM_FTR_INBOX, &v) == errNone && v;
|
||||
}
|
||||
|
||||
/* One beam: true if it's done with (added, or not something we read); *keep for Later. */
|
||||
static Boolean Handle(const Char *link, UInt16 len, Boolean *keep)
|
||||
{
|
||||
VmShare s;
|
||||
Char name[VM_NAME_SIZE + 1], msg[48];
|
||||
UInt16 n, pick;
|
||||
Err err;
|
||||
|
||||
*keep = false;
|
||||
if (!VmShareParse(link, len, &s))
|
||||
return true;
|
||||
n = TxtUtf8ToPalm(s.name, s.nameLen, name, VM_NAME_SIZE - 1);
|
||||
name[n] = 0;
|
||||
if (s.kind == VM_SHARE_CONTACT) {
|
||||
pick = FrmCustomAlert(InboxContactAlert, name,
|
||||
s.type == VM_ADV_TYPE_REPEATER ? "repeater" : s.type == VM_ADV_TYPE_ROOM ? "room" :
|
||||
s.type == VM_ADV_TYPE_SENSOR ? "sensor" : "chat", "");
|
||||
if (pick == 2)
|
||||
return true;
|
||||
if (pick == 1 || !SessionIsConnected()) {
|
||||
if (pick == 0)
|
||||
FrmCustomAlert(SettingsErrorAlert, "Connect to the radio first. It's kept for when you do.", "", "");
|
||||
*keep = true;
|
||||
return false;
|
||||
}
|
||||
err = SessionAddContact(s.key, s.type, name);
|
||||
if (err == errNone)
|
||||
err = ContactsUpdate();
|
||||
if (err != errNone) {
|
||||
AppErrorText(err, msg);
|
||||
FrmCustomAlert(SettingsErrorAlert, msg, "", "");
|
||||
*keep = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
pick = FrmCustomAlert(InboxChannelAlert, name, "", "");
|
||||
if (pick == 2)
|
||||
return true;
|
||||
if (pick == 1) {
|
||||
*keep = true;
|
||||
return false;
|
||||
}
|
||||
if (!ChannelJoin(name, s.secret)) {
|
||||
*keep = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void BeamCheckInbox(void)
|
||||
{
|
||||
LocalID id = DmFindDatabase(0, VM_INBOX_NAME);
|
||||
DmOpenRef db;
|
||||
MemHandle h;
|
||||
Char link[VM_BEAM_MAX];
|
||||
UInt16 i, len;
|
||||
Boolean keep;
|
||||
|
||||
FtrUnregister(VM_CREATOR, VM_FTR_INBOX);
|
||||
if (!id || !(db = DmOpenDatabase(0, id, dmModeReadWrite)))
|
||||
return;
|
||||
for (i = 0; i < DmNumRecords(db); ) {
|
||||
h = DmQueryRecord(db, i);
|
||||
if (!h) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
len = (UInt16)MemHandleSize(h);
|
||||
if (len >= sizeof(link))
|
||||
len = sizeof(link) - 1;
|
||||
MemMove(link, MemHandleLock(h), len);
|
||||
MemHandleUnlock(h);
|
||||
link[len] = 0;
|
||||
if (Handle(link, len, &keep))
|
||||
DmRemoveRecord(db, i);
|
||||
else if (keep)
|
||||
break; /* Later: ask again next launch */
|
||||
else
|
||||
i++;
|
||||
}
|
||||
DmCloseDatabase(db);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Beaming contacts and channels to another Visor (or any Palm running
|
||||
* VisorMesh) over IR, as meshcore:// links (protocol2.h), the same links
|
||||
* MeshCore One and the other apps share as QR codes. What arrives waits in
|
||||
* an inbox database until the app asks whether to add it.
|
||||
*/
|
||||
#ifndef VM_BEAM_H
|
||||
#define VM_BEAM_H
|
||||
|
||||
#include <PalmOS.h>
|
||||
#include "sections.h" /* this module is in the fifth code segment; BeamReceive in the first */
|
||||
#include "protocol.h"
|
||||
|
||||
#define VM_BEAM_MAX 320 /* bytes: the longest link, with room */
|
||||
#define VM_INBOX_NAME "VisorMeshInbox"
|
||||
#define VM_FTR_INBOX 2 /* feature: set when a beam arrives */
|
||||
#define VM_BEAM_EXT "vmsh"
|
||||
|
||||
/* sysAppLaunchCmdExgReceiveData, with or without globals (beam_rx.c, segment 1). */
|
||||
Err BeamReceive(ExgSocketPtr s);
|
||||
|
||||
void BeamContact(const VmContact *c) VM_SECT5;
|
||||
/* Our own contact, from the radio's SELF_INFO. */
|
||||
void BeamSelf(void) VM_SECT5;
|
||||
void BeamChannel(UInt8 slot) VM_SECT5;
|
||||
/* At launch: notes an inbox left from before, so BeamInboxWaiting sees it. */
|
||||
void BeamInboxNote(void) VM_SECT5;
|
||||
Boolean BeamInboxWaiting(void) VM_SECT5;
|
||||
/* Asks about each beam received: add it, keep it for later, or discard it. */
|
||||
void BeamCheckInbox(void) VM_SECT5;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Receiving a beam (sysAppLaunchCmdExgReceiveData). That launch can come
|
||||
* while the app isn't running, without globals and with only the first code
|
||||
* segment, so this stays in segment 1, touches no globals and calls nothing
|
||||
* in another segment: it only saves what came to the inbox database and
|
||||
* sets a feature. The app asks what to do with it when it next runs
|
||||
* (beam.c, BeamCheckInbox).
|
||||
*/
|
||||
#include <PalmOS.h>
|
||||
#include "app.h"
|
||||
#include "beam.h"
|
||||
|
||||
#define INBOX_TYPE 'inbx'
|
||||
|
||||
Err BeamReceive(ExgSocketPtr s)
|
||||
{
|
||||
UInt8 buf[VM_BEAM_MAX];
|
||||
UInt32 n = 0, got;
|
||||
LocalID id;
|
||||
DmOpenRef db;
|
||||
MemHandle h;
|
||||
UInt16 at = dmMaxRecordIndex;
|
||||
Err err;
|
||||
|
||||
err = ExgAccept(s);
|
||||
if (err != errNone)
|
||||
return err;
|
||||
do {
|
||||
got = ExgReceive(s, buf + n, VM_BEAM_MAX - n, &err);
|
||||
n += got;
|
||||
} while (got > 0 && err == errNone && n < VM_BEAM_MAX);
|
||||
ExgDisconnect(s, err);
|
||||
if (err != errNone || n == 0 || n >= VM_BEAM_MAX)
|
||||
return err;
|
||||
id = DmFindDatabase(0, VM_INBOX_NAME);
|
||||
if (!id) {
|
||||
err = DmCreateDatabase(0, VM_INBOX_NAME, VM_CREATOR, INBOX_TYPE, false);
|
||||
if (err != errNone)
|
||||
return err;
|
||||
id = DmFindDatabase(0, VM_INBOX_NAME);
|
||||
}
|
||||
db = id ? DmOpenDatabase(0, id, dmModeReadWrite) : 0;
|
||||
if (!db)
|
||||
return dmErrCantOpen;
|
||||
h = DmNewRecord(db, &at, n);
|
||||
if (h) {
|
||||
DmWrite(MemHandleLock(h), 0, buf, n);
|
||||
MemHandleUnlock(h);
|
||||
DmReleaseRecord(db, at, true);
|
||||
}
|
||||
DmCloseDatabase(db);
|
||||
FtrSet(VM_CREATOR, VM_FTR_INBOX, 1);
|
||||
return h ? errNone : dmErrMemError;
|
||||
}
|
||||
@@ -20,6 +20,7 @@ static const Char *FieldText(FormType *form, UInt16 id) VM_SECT4;
|
||||
static Boolean IsPrivate(FormType *form) VM_SECT4;
|
||||
static void ShowKeyField(FormType *form) VM_SECT4;
|
||||
static void Error(const Char *msg) VM_SECT4;
|
||||
static Boolean HashtagKey(const Char *name, UInt8 *key) VM_SECT4;
|
||||
static Boolean Add(FormType *form) VM_SECT4;
|
||||
|
||||
static void *GetObject(FormType *form, UInt16 id)
|
||||
@@ -58,14 +59,27 @@ static void Error(const Char *msg)
|
||||
FrmCustomAlert(SettingsErrorAlert, msg, "", "");
|
||||
}
|
||||
|
||||
static Boolean Add(FormType *form)
|
||||
/* The key for a #hashtag channel: the first 16 bytes of SHA-256 of its exact name, '#' included. */
|
||||
static Boolean HashtagKey(const Char *name, UInt8 *key)
|
||||
{
|
||||
const Char *typed = FieldText(form, ChannelNameField);
|
||||
Char name[VM_NAME_SIZE + 1], msg[48];
|
||||
UInt8 key[VM_CHANNEL_SECRET_SIZE], utf8[VM_NAME_SIZE], hash[32];
|
||||
UInt8 utf8[VM_NAME_SIZE], hash[32];
|
||||
UInt16 n = TxtPalmToUtf8(name, StrLen(name), utf8, VM_NAME_SIZE - 1);
|
||||
|
||||
if (n == 0)
|
||||
return false;
|
||||
VmSha256(utf8, n, hash);
|
||||
MemMove(key, hash, VM_CHANNEL_SECRET_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
Boolean ChannelJoin(const Char *name, const UInt8 *secret)
|
||||
{
|
||||
UInt8 key[VM_CHANNEL_SECRET_SIZE];
|
||||
UInt8 maxChannels = SessionDevice()->maxChannels;
|
||||
UInt16 n;
|
||||
Char msg[48];
|
||||
Int16 slot;
|
||||
UInt16 i;
|
||||
VmChannel c;
|
||||
VmConv conv;
|
||||
Err err;
|
||||
|
||||
@@ -73,27 +87,22 @@ static Boolean Add(FormType *form)
|
||||
Error("Connect to the radio first.");
|
||||
return false;
|
||||
}
|
||||
if (!*typed) {
|
||||
Error("Enter a channel name.");
|
||||
if (secret)
|
||||
MemMove(key, secret, sizeof(key));
|
||||
else if (!HashtagKey(name, key)) {
|
||||
Error("That name is too long.");
|
||||
return false;
|
||||
}
|
||||
if (IsPrivate(form)) {
|
||||
StrNCopy(name, typed, VM_NAME_SIZE - 1);
|
||||
name[VM_NAME_SIZE - 1] = 0;
|
||||
if (!VmParseHex(FieldText(form, ChannelKeyField), key, sizeof(key))) {
|
||||
Error("The key is 32 hex digits (0-9, a-f).");
|
||||
return false;
|
||||
MemSet(&conv, sizeof(conv), 0);
|
||||
conv.type = VM_CONV_CHANNEL;
|
||||
ChannelsOpen(); /* opened lazily (AGENTS.md): this can come before Home has shown */
|
||||
/* already joined (perhaps by another name): open it */
|
||||
for (i = 0; i < maxChannels; i++) {
|
||||
if (ChannelsFindSlot((UInt8)i, &c) && MemCmp(c.secret, key, sizeof(key)) == 0) {
|
||||
conv.channel = (UInt8)i;
|
||||
AppOpenConversation(&conv);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
/* hashtag: the key is derived from the exact name, '#' included */
|
||||
StrPrintF(name, "%s%s", typed[0] == '#' ? "" : "#", typed);
|
||||
n = TxtPalmToUtf8(name, StrLen(name), utf8, VM_NAME_SIZE - 1);
|
||||
if (n == 0) {
|
||||
Error("That name is too long.");
|
||||
return false;
|
||||
}
|
||||
VmSha256(utf8, n, hash);
|
||||
MemMove(key, hash, sizeof(key));
|
||||
}
|
||||
slot = ChannelsFreeSlot(maxChannels);
|
||||
if (slot < 0) {
|
||||
@@ -107,13 +116,35 @@ static Boolean Add(FormType *form)
|
||||
Error(msg);
|
||||
return false;
|
||||
}
|
||||
MemSet(&conv, sizeof(conv), 0);
|
||||
conv.type = VM_CONV_CHANNEL;
|
||||
conv.channel = (UInt8)slot;
|
||||
AppOpenConversation(&conv);
|
||||
return true;
|
||||
}
|
||||
|
||||
static Boolean Add(FormType *form)
|
||||
{
|
||||
const Char *typed = FieldText(form, ChannelNameField);
|
||||
Char name[VM_NAME_SIZE + 1];
|
||||
UInt8 key[VM_CHANNEL_SECRET_SIZE];
|
||||
|
||||
if (!*typed) {
|
||||
Error("Enter a channel name.");
|
||||
return false;
|
||||
}
|
||||
if (IsPrivate(form)) {
|
||||
StrNCopy(name, typed, VM_NAME_SIZE - 1);
|
||||
name[VM_NAME_SIZE - 1] = 0;
|
||||
if (!VmParseHex(FieldText(form, ChannelKeyField), key, sizeof(key))) {
|
||||
Error("The key is 32 hex digits (0-9, a-f).");
|
||||
return false;
|
||||
}
|
||||
return ChannelJoin(name, key);
|
||||
}
|
||||
/* hashtag: the key is derived from the exact name, '#' included */
|
||||
StrPrintF(name, "%s%s", typed[0] == '#' ? "" : "#", typed);
|
||||
return ChannelJoin(name, NULL);
|
||||
}
|
||||
|
||||
Boolean ChannelFormHandleEvent(EventType *event)
|
||||
{
|
||||
FormType *form = FrmGetActiveForm();
|
||||
|
||||
@@ -5,6 +5,12 @@
|
||||
#include "sections.h" /* this module is in the fourth code segment */
|
||||
|
||||
Boolean ChannelFormHandleEvent(EventType *event) VM_SECT4;
|
||||
/*
|
||||
* Joins a channel and opens it: secret NULL for a #hashtag (the key comes
|
||||
* from the name). One already joined with that key just opens. Errors show
|
||||
* in an alert; false if it wasn't joined.
|
||||
*/
|
||||
Boolean ChannelJoin(const Char *name, const UInt8 *secret) VM_SECT4;
|
||||
/* Options > Leave Channel: asks, then empties the open channel's slot. */
|
||||
void ChannelLeaveCurrent(void) VM_SECT4;
|
||||
/* The same for any channel (Home's tap-and-hold); goes back to Home. */
|
||||
|
||||
Reference in new issue
Block a user