mirror of
https://github.com/meshtastic/firmware.git
synced 2025-06-10 23:22:05 +00:00
allow observers to return an error code to abort further processing
Will allow me to use observers to generalize the various hooks that need to run to preflight sleep entry.
This commit is contained in:
parent
6ad451eb5f
commit
640cb3bf7f
@ -322,7 +322,7 @@ void MeshService::sendOurPosition(NodeNum dest, bool wantReplies)
|
||||
sendToMesh(p);
|
||||
}
|
||||
|
||||
void MeshService::onGPSChanged(void *unused)
|
||||
int MeshService::onGPSChanged(void *unused)
|
||||
{
|
||||
DEBUG_MSG("got gps notify\n");
|
||||
|
||||
@ -354,4 +354,6 @@ void MeshService::onGPSChanged(void *unused)
|
||||
|
||||
releaseToPool(p);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -80,7 +80,8 @@ class MeshService
|
||||
void sendToMesh(MeshPacket *p);
|
||||
|
||||
/// Called when our gps position has changed - updates nodedb and sends Location message out into the mesh
|
||||
void onGPSChanged(void *arg);
|
||||
/// returns 0 to allow futher processing
|
||||
int onGPSChanged(void *arg);
|
||||
|
||||
/// handle all the packets that just arrived from the mesh radio
|
||||
void handleFromRadio();
|
||||
|
@ -24,7 +24,11 @@ template <class T> class Observer
|
||||
friend class Observable<T>;
|
||||
|
||||
protected:
|
||||
virtual void onNotify(T arg) = 0;
|
||||
/**
|
||||
* returns 0 if other observers should continue to be called
|
||||
* returns !0 if the observe calls should be aborted and this result code returned for notifyObservers
|
||||
**/
|
||||
virtual int onNotify(T arg) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -32,7 +36,7 @@ template <class T> class Observer
|
||||
*/
|
||||
template <class Callback, class T> class CallbackObserver : public Observer<T>
|
||||
{
|
||||
typedef void (Callback::*ObserverCallback)(T arg);
|
||||
typedef int (Callback::*ObserverCallback)(T arg);
|
||||
|
||||
Callback *objPtr;
|
||||
ObserverCallback method;
|
||||
@ -41,7 +45,7 @@ template <class Callback, class T> class CallbackObserver : public Observer<T>
|
||||
CallbackObserver(Callback *_objPtr, ObserverCallback _method) : objPtr(_objPtr), method(_method) {}
|
||||
|
||||
protected:
|
||||
virtual void onNotify(T arg) { (objPtr->*method)(arg); }
|
||||
virtual int onNotify(T arg) { return (objPtr->*method)(arg); }
|
||||
};
|
||||
|
||||
/**
|
||||
@ -55,13 +59,19 @@ template <class T> class Observable
|
||||
public:
|
||||
/**
|
||||
* Tell all observers about a change, observers can process arg as they wish
|
||||
*
|
||||
* returns !0 if an observer chose to abort processing by returning this code
|
||||
*/
|
||||
void notifyObservers(T arg)
|
||||
int notifyObservers(T arg)
|
||||
{
|
||||
for (typename std::list<Observer<T> *>::const_iterator iterator = observers.begin(); iterator != observers.end();
|
||||
++iterator) {
|
||||
(*iterator)->onNotify(arg);
|
||||
int result = (*iterator)->onNotify(arg);
|
||||
if (result != 0)
|
||||
return result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -222,10 +222,11 @@ class FromNumCharacteristic : public CallbackCharacteristic, public Observer<uin
|
||||
}
|
||||
|
||||
/// If the mesh service tells us fromNum has changed, tell the phone
|
||||
virtual void onNotify(uint32_t newValue)
|
||||
virtual int onNotify(uint32_t newValue)
|
||||
{
|
||||
setValue(newValue);
|
||||
notify();
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user