├── .cproject ├── .gitignore ├── .project ├── .vscode ├── c_cpp_properties.json └── settings.json ├── LICENSE ├── Makefile ├── README.md └── src ├── HouseholdSolarPowerGeneration └── SampleExample.cpp ├── openecho ├── DeviceObject.cpp ├── DeviceObject.h ├── Echo.cpp ├── Echo.h ├── EchoFrame.cpp ├── EchoFrame.h ├── EchoNode.cpp ├── EchoNode.h ├── EchoObject.cpp ├── EchoObject.h ├── EchoProperty.cpp ├── EchoProperty.h ├── EchoProtocol.cpp ├── EchoProtocol.h ├── EchoSocket.cpp ├── EchoSocket.h ├── EchoStorage.cpp ├── EchoStorage.h ├── EchoUDPProtocol.cpp ├── EchoUDPProtocol.h ├── EchoUtils.cpp ├── EchoUtils.h ├── HouseholdSolarPowerGeneration.cpp ├── HouseholdSolarPowerGeneration.h ├── NodeProfile.cpp ├── NodeProfile.h ├── OpenECHO.h ├── ProfileObject.cpp ├── ProfileObject.h ├── SmartElectricEnergyMeter.cpp └── SmartElectricEnergyMeter.h ├── tutorial1 └── main.cpp ├── tutorial2a └── main.cpp └── tutorial4 ├── main ├── main.cpp └── man.cpp /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 88 | 89 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | Thumbs.db 4 | *.class 5 | *.dex 6 | /bin/ 7 | /gen/ 8 | /Debug/ -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | OpenECHO 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "compilerPath": "/usr/bin/gcc", 10 | "cStandard": "c17", 11 | "cppStandard": "gnu++17", 12 | "intelliSenseMode": "linux-gcc-arm" 13 | } 14 | ], 15 | "version": 4 16 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "array": "cpp", 4 | "atomic": "cpp", 5 | "bit": "cpp", 6 | "*.tcc": "cpp", 7 | "cctype": "cpp", 8 | "clocale": "cpp", 9 | "cmath": "cpp", 10 | "compare": "cpp", 11 | "concepts": "cpp", 12 | "cstdarg": "cpp", 13 | "cstddef": "cpp", 14 | "cstdint": "cpp", 15 | "cstdio": "cpp", 16 | "cstdlib": "cpp", 17 | "cstring": "cpp", 18 | "cwchar": "cpp", 19 | "cwctype": "cpp", 20 | "deque": "cpp", 21 | "list": "cpp", 22 | "map": "cpp", 23 | "set": "cpp", 24 | "string": "cpp", 25 | "unordered_map": "cpp", 26 | "vector": "cpp", 27 | "exception": "cpp", 28 | "algorithm": "cpp", 29 | "functional": "cpp", 30 | "iterator": "cpp", 31 | "memory": "cpp", 32 | "memory_resource": "cpp", 33 | "numeric": "cpp", 34 | "random": "cpp", 35 | "string_view": "cpp", 36 | "system_error": "cpp", 37 | "tuple": "cpp", 38 | "type_traits": "cpp", 39 | "utility": "cpp", 40 | "initializer_list": "cpp", 41 | "iosfwd": "cpp", 42 | "iostream": "cpp", 43 | "istream": "cpp", 44 | "limits": "cpp", 45 | "new": "cpp", 46 | "numbers": "cpp", 47 | "ostream": "cpp", 48 | "stdexcept": "cpp", 49 | "streambuf": "cpp", 50 | "cinttypes": "cpp", 51 | "typeinfo": "cpp" 52 | } 53 | } -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # This is makefile for GNU Make and linux. 2 | # confirm with g++ 4.8.x 3 | CPP = g++ 4 | CFLAGS = -O2 -std=c++11 -Wall 5 | LDFLAGS = 6 | LIBS = -lpthread 7 | CPP_FILES = src/OpenECHO.cpp $(wildcard src/echo/*.cpp) 8 | # TODO: fix this. 9 | OBJ_DIR = obj 10 | OBJS = $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o))) 11 | 12 | PROGRAM = OpenECHOForCpp 13 | 14 | all: $(PROGRAM) 15 | 16 | $(PROGRAM): directories $(OBJS) 17 | $(CPP) $(OBJS) $(CFLAGS) $(LDFLAGS) $(LIBS) -o $(PROGRAM) 18 | 19 | obj/%.o: src/%.cpp 20 | $(CPP) $(CFLAGS) -c -o $@ $< 21 | 22 | obj/%.o: src/echo/%.cpp 23 | $(CPP) $(CFLAGS) -c -o $@ $< 24 | 25 | clean:; 26 | find . -type f -name "*.o" | xargs rm -f 27 | rm -r obj 28 | 29 | PHONY: check-syntax 30 | check-syntax: 31 | $(CPP) -Wall -fsyntax-only $(LDFLAGS) $(CFLAGS) $(LIBS) $(CHK_SOURCES) 32 | 33 | PHONY: directories 34 | directories: $(OBJ_DIR) 35 | 36 | $(OBJ_DIR): 37 | mkdir $(OBJ_DIR) 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenECHO-cpp 2 | ============ 3 | 4 | C++ Implementation of ECHONET Lite 5 | 6 | ###概要 7 | 8 | OpenECHO-cppは、家電やセンサーデバイスなど、スマートハウスで用いられる機器のための通信プロトコルである[ECHONET Lite][]をC++で実装したドライバライブラリです。 9 | 同Java版の[OpenECHO][]と兄弟的なライブラリになりますが、言語以外にもいくつかの相違点があります。一番大きな違いは、C++版には機器オブジェクトがなく、使う機器のクラスを自分で生成する必要があるということです。面倒さと引き換えに、サイズの小ささを獲得しています。 10 | 11 | 12 | 現在、ECHONET Lite対応機器としてECHONETコンソーシアムに認証された機器のリストが[こちらにあります](http://www.echonet.gr.jp/kikaku_ninsyo/list_lite/equip_srch)。2014年4月時点ですでに160種類以上の機器が受理されている模様です。 13 | 14 | ※ただし、あくまでコンソーシアムが認証した機器のリストであって、すでに市場に出回っているかどうかはわかりません。OpenECHOもこれら全ての機器の動作を保証するものではありません。というか、どの機器の動作も保証いたしません。 15 | 16 | Java版を用いて実装されたAndroidホームサーバー「Kadecot」による[動作実験ビデオ](http://www.youtube.com/watch?v=SwpHSAvoV9I)があります。 17 | 18 | ###ライセンス 19 | 本ソフトウェアの著作権は[株式会社ソニーコンピュータサイエンス研究所][]が保持しており、GPLで配布されています。ライセンスに従い,自由にご利用ください。 20 | 21 | ###用いているデータベース 22 | 本ライブラリの作成には、弊社から公開されている[ECHONET Liteデータベース][]を用いています。 23 | [ECHONET Liteデータベース][]の最新仕様へのアップデート・やフィードバックも随時募集しています。データベースのライセンスはパブリックドメインですのでぜひご協力ください。 24 | 25 | ###互換性と動作レポート 26 | OpenECHO-cppは標準的なC++の機能だけで実装されておりますが、必ずどこでも動くわけではありません。動作チェックは行っておらず動く保証はありません。ご了承ください。 27 | 28 | [ECHONET Lite]: http://www.echonet.gr.jp/ "ECHONET Lite" 29 | [OpenECHO]: https://github.com/SonyCSL/OpenECHO "OpenECHO" 30 | [株式会社ソニーコンピュータサイエンス研究所]: http://www.sonycsl.co.jp/ "株式会社ソニーコンピュータサイエンス研究所" 31 | [MITライセンス]: http://opensource.org/licenses/mit-license.php "MITライセンス" 32 | [Processing]: http://processing.org "Processing" 33 | [神奈川工科大学スマートハウス研究センター]: http://smarthouse-center.org/sdk/ "神奈川工科大学スマートハウス研究センター" 34 | [ECHONET Liteデータベース]: https://github.com/SonyCSL/ECHONETLite-ObjectDatabase "ECHONET Liteデータベース" 35 | -------------------------------------------------------------------------------- /src/HouseholdSolarPowerGeneration/SampleExample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../openecho/OpenECHO.h" 4 | #include "../openecho/HouseholdSolarPowerGeneration.h" 5 | #include "../openecho/EchoProperty.h" 6 | 7 | using namespace std; 8 | using namespace sonycsl_openecho; 9 | class DefaultNodeProfile : public NodeProfile 10 | { 11 | 12 | public: 13 | DefaultNodeProfile() : NodeProfile() {} 14 | virtual ~DefaultNodeProfile() {} 15 | 16 | protected: 17 | virtual shared_ptr> getManufacturerCode() 18 | { 19 | return shared_ptr>(); 20 | } 21 | virtual shared_ptr> getOperatingStatus() 22 | { 23 | return shared_ptr>(); 24 | } 25 | virtual shared_ptr> getIdentificationNumber() 26 | { 27 | return shared_ptr>(); 28 | } 29 | }; 30 | class Household : public HouseholdSolarPowerGeneration 31 | { 32 | protected: 33 | shared_ptr> mStatus; 34 | shared_ptr> mLocation; 35 | shared_ptr> mFaultStatus; 36 | shared_ptr> mManufacturerCode; 37 | static const unsigned char EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION = 0xE0; 38 | static const unsigned char EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION = 0xE1; 39 | 40 | public: 41 | Household() : HouseholdSolarPowerGeneration() 42 | { 43 | mStatus = shared_ptr>(new vector()); 44 | mStatus.get()->push_back(0x30); 45 | mLocation = shared_ptr>(new vector()); 46 | mLocation.get()->push_back(0x00); 47 | mFaultStatus = shared_ptr>(new vector()); 48 | mFaultStatus.get()->push_back(0x42); 49 | mManufacturerCode = shared_ptr>(new vector()); 50 | mManufacturerCode.get()->push_back(0x00); 51 | mManufacturerCode.get()->push_back(0x00); 52 | mManufacturerCode.get()->push_back(0x00); 53 | } 54 | virtual ~Household() {} 55 | virtual unsigned short getEchoClassCode() 56 | { 57 | return 0x0279; 58 | } 59 | 60 | protected: 61 | virtual void setupPropertyMaps() 62 | { 63 | HouseholdSolarPowerGeneration::setupPropertyMaps(); 64 | addSetProperty(EPC_OPERATION_STATUS); 65 | addGetProperty(EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION); 66 | addGetProperty(EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION); 67 | } 68 | virtual bool setProperty(EchoProperty& property) { 69 | 70 | bool success = DeviceObject::setProperty(property); 71 | if(success) return true; 72 | 73 | switch(property.epc) { 74 | default : return false; 75 | } 76 | } 77 | 78 | virtual std::shared_ptr> getProperty( 79 | unsigned char epc) 80 | { 81 | 82 | std::shared_ptr> edt = HouseholdSolarPowerGeneration::getProperty(epc); 83 | if (edt.get() != nullptr) 84 | return edt; 85 | 86 | switch (epc) 87 | { 88 | case EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION: 89 | return getMeasuredInstantaneousAmountOfElectricityGenerated(); 90 | case EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION: 91 | return getMeasuredCumulativeAmountOfElectricityGenerated(); 92 | default: 93 | return std::shared_ptr>(); 94 | } 95 | } 96 | 97 | virtual shared_ptr> getMeasuredInstantaneousAmountOfElectricityGenerated() 98 | { 99 | cout << "getMeasuredInstantaneousAmountOfElectricityGenerated function call" << endl; 100 | return NULL; 101 | } 102 | virtual shared_ptr> getMeasuredCumulativeAmountOfElectricityGenerated() 103 | { 104 | cout << "getMeasuredCumulativeAmountOfElectricityGenerated function call" << endl; 105 | 106 | return NULL; 107 | } 108 | 109 | virtual bool isValidProperty(EchoProperty &property) 110 | { 111 | 112 | bool valid = HouseholdSolarPowerGeneration::isValidProperty(property); 113 | if (valid) 114 | return true; 115 | 116 | switch (property.epc) 117 | { 118 | case EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION: 119 | return isValidMeasuredInstantaneousAmountOfElectricityGenerated(property.edt); 120 | case EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION: 121 | return isValidMeasuredCumulativeAmountOfElectricityGenerated(property.edt); 122 | default: 123 | return false; 124 | } 125 | } 126 | bool setOperationStatus(vector &edt) 127 | { 128 | (*(mStatus.get()))[0] = edt.at(0); 129 | if (mStatus.get()->at(0) == 0x30) 130 | { 131 | cout << "ON" << endl; 132 | } 133 | else 134 | { 135 | cout << "OFF" << endl; 136 | } 137 | HouseholdSolarPowerGeneration::inform().reqInformOperationStatus().send(); 138 | return true; 139 | } 140 | virtual shared_ptr> getOperationStatus() 141 | { 142 | return mStatus; 143 | } 144 | virtual bool setInstallationLocation(vector &edt) 145 | { 146 | return false; 147 | } 148 | virtual shared_ptr> getInstallationLocation() 149 | { 150 | return mLocation; 151 | } 152 | virtual shared_ptr> getFaultStatus() 153 | { 154 | return mFaultStatus; 155 | } 156 | virtual shared_ptr> getManufacturerCode() 157 | { 158 | return mManufacturerCode; 159 | } 160 | }; 161 | 162 | int main() 163 | { 164 | 165 | shared_ptr profile(new DefaultNodeProfile()); 166 | vector> devices; 167 | devices.push_back(shared_ptr(new Household())); 168 | 169 | // Echo::addEventListener(std::shared_ptr(new Echo::Logger())); 170 | 171 | Echo::start(profile, devices); 172 | 173 | while (true) 174 | { 175 | NodeProfile::Getter(NodeProfile::ECHO_CLASS_CODE, NodeProfile::INSTANCE_CODE, EchoSocket::MULTICAST_ADDRESS).reqGetSelfNodeInstanceListS().send(); 176 | 177 | sleep(100); 178 | } 179 | 180 | return 0; 181 | } -------------------------------------------------------------------------------- /src/openecho/DeviceObject.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DeviceObject.h 3 | * 4 | * Created on: 2013/10/29 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef DEVICEOBJECT_H_ 9 | #define DEVICEOBJECT_H_ 10 | 11 | #include "EchoObject.h" 12 | 13 | namespace sonycsl_openecho { 14 | 15 | class DeviceObject: public EchoObject { 16 | public: 17 | class Receiver; 18 | class Setter; 19 | class Getter; 20 | class Informer; 21 | class InformerC; 22 | class Proxy; 23 | public: 24 | static const unsigned char EPC_OPERATION_STATUS; 25 | static const unsigned char EPC_INSTALLATION_LOCATION; 26 | static const unsigned char EPC_STANDARD_VERSION_INFORMATION; 27 | static const unsigned char EPC_IDENTIFICATION_NUMBER; 28 | static const unsigned char EPC_MEASURED_INSTANTANEOUS_POWER_CONSUMPTION; 29 | static const unsigned char EPC_MEASURED_CUMULATIVE_POWER_CONSUMPTION; 30 | static const unsigned char EPC_MANUFACTURERS_FAULT_CODE; 31 | static const unsigned char EPC_CURRENT_LIMIT_SETTING; 32 | static const unsigned char EPC_FAULT_STATUS; 33 | static const unsigned char EPC_FAULT_DESCRIPTION; 34 | static const unsigned char EPC_MANUFACTURER_CODE; 35 | static const unsigned char EPC_BUSINESS_FACILITY_CODE; 36 | static const unsigned char EPC_PRODUCT_CODE; 37 | static const unsigned char EPC_PRODUCTION_NUMBER; 38 | static const unsigned char EPC_PRODUCTION_DATE; 39 | static const unsigned char EPC_POWER_SAVING_OPERATION_SETTING; 40 | static const unsigned char EPC_REMOTE_CONTROL_SETTING; 41 | static const unsigned char EPC_CURRENT_TIME_SETTING; 42 | static const unsigned char EPC_CURRENT_DATE_SETTING; 43 | static const unsigned char EPC_POWER_LIMIT_SETTING; 44 | static const unsigned char EPC_CUMULATIVE_OPERATING_TIME; 45 | static const unsigned char EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP; 46 | static const unsigned char EPC_SET_PROPERTY_MAP; 47 | static const unsigned char EPC_GET_PROPERTY_MAP; 48 | 49 | protected: 50 | unsigned char mEchoInstanceCode; 51 | std::shared_ptr > mStandardVersionInformation; 52 | public: 53 | DeviceObject(); 54 | virtual ~DeviceObject(); 55 | 56 | public: 57 | virtual unsigned char getInstanceCode(); 58 | 59 | virtual void onNew(std::shared_ptr eoj); 60 | 61 | virtual void allocateSelfDeviceInstanceCode(); 62 | 63 | virtual bool setProperty(EchoProperty& property); 64 | virtual std::shared_ptr > getProperty(unsigned char epc); 65 | virtual bool isValidProperty(EchoProperty& property); 66 | 67 | DeviceObject::Setter set(); 68 | DeviceObject::Setter set(bool responseRequired); 69 | DeviceObject::Getter get(); 70 | DeviceObject::Informer inform(); 71 | 72 | protected: 73 | DeviceObject::Informer inform(bool multicast); 74 | DeviceObject::InformerC informC(std::string address); 75 | 76 | virtual void setupPropertyMaps(); 77 | 78 | virtual bool setOperationStatus(std::vector& edt); 79 | virtual std::shared_ptr > getOperationStatus() = 0; 80 | virtual bool isValidOperationStatus(std::vector& edt); 81 | virtual bool setInstallationLocation(std::vector& edt) = 0; 82 | virtual std::shared_ptr > getInstallationLocation() = 0; 83 | virtual bool isValidInstallationLocation(std::vector& edt); 84 | virtual std::shared_ptr > getStandardVersionInformation(); 85 | virtual bool isValidStandardVersionInformation(std::vector& edt); 86 | virtual std::shared_ptr > getIdentificationNumber(); 87 | virtual bool isValidIdentificationNumber(std::vector& edt); 88 | virtual std::shared_ptr > getMeasuredInstantaneousPowerConsumption(); 89 | virtual bool isValidMeasuredInstantaneousPowerConsumption(std::vector& edt); 90 | virtual std::shared_ptr > getMeasuredCumulativePowerConsumption(); 91 | virtual bool isValidMeasuredCumulativePowerConsumption(std::vector& edt); 92 | virtual std::shared_ptr > getManufacturersFaultCode(); 93 | virtual bool isValidManufacturersFaultCode(std::vector& edt); 94 | virtual bool setCurrentLimitSetting(std::vector& edt); 95 | virtual std::shared_ptr > getCurrentLimitSetting(); 96 | virtual bool isValidCurrentLimitSetting(std::vector& edt); 97 | virtual std::shared_ptr > getFaultStatus() = 0; 98 | virtual bool isValidFaultStatus(std::vector& edt); 99 | virtual std::shared_ptr > getFaultDescription(); 100 | virtual bool isValidFaultDescription(std::vector& edt); 101 | virtual std::shared_ptr > getManufacturerCode() = 0; 102 | virtual bool isValidManufacturerCode(std::vector& edt); 103 | virtual std::shared_ptr > getBusinessFacilityCode(); 104 | virtual bool isValidBusinessFacilityCode(std::vector& edt); 105 | virtual std::shared_ptr > getProductCode(); 106 | virtual bool isValidProductCode(std::vector& edt); 107 | virtual std::shared_ptr > getProductionNumber(); 108 | virtual bool isValidProductionNumber(std::vector& edt); 109 | virtual std::shared_ptr > getProductionDate(); 110 | virtual bool isValidProductionDate(std::vector& edt); 111 | virtual bool setPowerSavingOperationSetting(std::vector& edt); 112 | virtual std::shared_ptr > getPowerSavingOperationSetting(); 113 | virtual bool isValidPowerSavingOperationSetting(std::vector& edt); 114 | virtual bool setRemoteControlSetting(std::vector& edt); 115 | virtual std::shared_ptr > getRemoteControlSetting(); 116 | virtual bool isValidRemoteControlSetting(std::vector& edt); 117 | virtual bool setCurrentTimeSetting(std::vector& edt); 118 | virtual std::shared_ptr > getCurrentTimeSetting(); 119 | virtual bool isValidCurrentTimeSetting(std::vector& edt); 120 | virtual bool setCurrentDateSetting(std::vector& edt); 121 | virtual std::shared_ptr > getCurrentDateSetting(); 122 | virtual bool isValidCurrentDateSetting(std::vector& edt); 123 | virtual bool setPowerLimitSetting(std::vector& edt); 124 | virtual std::shared_ptr > getPowerLimitSetting(); 125 | virtual bool isValidPowerLimitSetting(std::vector& edt); 126 | virtual std::shared_ptr > getCumulativeOperatingTime(); 127 | virtual bool isValidCumulativeOperatingTime(std::vector& edt); 128 | virtual std::shared_ptr > getStatusChangeAnnouncementPropertyMap(); 129 | virtual bool isValidStatusChangeAnnouncementPropertyMap(std::vector& edt); 130 | virtual std::shared_ptr > getSetPropertyMap(); 131 | virtual bool isValidSetPropertyMap(std::vector& edt); 132 | virtual std::shared_ptr > getGetPropertyMap(); 133 | virtual bool isValidGetPropertyMap(std::vector& edt); 134 | 135 | public: 136 | class Receiver : public EchoObject::Receiver { 137 | public: 138 | Receiver(); 139 | virtual ~Receiver(); 140 | protected: 141 | virtual bool onSetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 142 | virtual bool onGetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 143 | virtual bool onInformProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property); 144 | 145 | virtual void onSetOperationStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 146 | virtual void onSetInstallationLocation(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 147 | virtual void onSetCurrentLimitSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 148 | virtual void onSetPowerSavingOperationSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 149 | virtual void onSetRemoteControlSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 150 | virtual void onSetCurrentTimeSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 151 | virtual void onSetCurrentDateSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 152 | virtual void onSetPowerLimitSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 153 | 154 | virtual void onGetOperationStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 155 | virtual void onGetInstallationLocation(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 156 | virtual void onGetStandardVersionInformation(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 157 | virtual void onGetIdentificationNumber(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 158 | virtual void onGetMeasuredInstantaneousPowerConsumption(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 159 | virtual void onGetMeasuredCumulativePowerConsumption(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 160 | virtual void onGetManufacturersFaultCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 161 | virtual void onGetCurrentLimitSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 162 | virtual void onGetFaultStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 163 | virtual void onGetFaultDescription(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 164 | virtual void onGetManufacturerCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 165 | virtual void onGetBusinessFacilityCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 166 | virtual void onGetProductCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 167 | virtual void onGetProductionNumber(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 168 | virtual void onGetProductionDate(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 169 | virtual void onGetPowerSavingOperationSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 170 | virtual void onGetRemoteControlSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 171 | virtual void onGetCurrentTimeSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 172 | virtual void onGetCurrentDateSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 173 | virtual void onGetPowerLimitSetting(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 174 | virtual void onGetCumulativeOperatingTime(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 175 | virtual void onGetStatusChangeAnnouncementPropertyMap(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 176 | virtual void onGetSetPropertyMap(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 177 | virtual void onGetGetPropertyMap(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 178 | 179 | }; 180 | 181 | class Setter : public EchoObject::Setter { 182 | public: 183 | Setter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 184 | , std::string dstEchoAddress, bool responseRequired); 185 | virtual ~Setter(); 186 | virtual DeviceObject::Setter& reqSetProperty(unsigned char epc, std::vector edt); 187 | 188 | virtual DeviceObject::Setter& reqSetOperationStatus(std::vector edt); 189 | virtual DeviceObject::Setter& reqSetInstallationLocation(std::vector edt); 190 | virtual DeviceObject::Setter& reqSetCurrentLimitSetting(std::vector edt); 191 | virtual DeviceObject::Setter& reqSetPowerSavingOperationSetting(std::vector edt); 192 | virtual DeviceObject::Setter& reqSetRemoteControlSetting(std::vector edt); 193 | virtual DeviceObject::Setter& reqSetCurrentTimeSetting(std::vector edt); 194 | virtual DeviceObject::Setter& reqSetCurrentDateSetting(std::vector edt); 195 | virtual DeviceObject::Setter& reqSetPowerLimitSetting(std::vector edt); 196 | 197 | }; 198 | 199 | class Getter : public EchoObject::Getter { 200 | public: 201 | Getter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 202 | , std::string dstEchoAddress); 203 | virtual ~Getter(); 204 | virtual DeviceObject::Getter& reqGetProperty(unsigned char epc); 205 | 206 | virtual DeviceObject::Getter& reqGetOperationStatus(); 207 | virtual DeviceObject::Getter& reqGetInstallationLocation(); 208 | virtual DeviceObject::Getter& reqGetStandardVersionInformation(); 209 | virtual DeviceObject::Getter& reqGetIdentificationNumber(); 210 | virtual DeviceObject::Getter& reqGetMeasuredInstantaneousPowerConsumption(); 211 | virtual DeviceObject::Getter& reqGetMeasuredCumulativePowerConsumption(); 212 | virtual DeviceObject::Getter& reqGetManufacturersFaultCode(); 213 | virtual DeviceObject::Getter& reqGetCurrentLimitSetting(); 214 | virtual DeviceObject::Getter& reqGetFaultStatus(); 215 | virtual DeviceObject::Getter& reqGetFaultDescription(); 216 | virtual DeviceObject::Getter& reqGetManufacturerCode(); 217 | virtual DeviceObject::Getter& reqGetBusinessFacilityCode(); 218 | virtual DeviceObject::Getter& reqGetProductCode(); 219 | virtual DeviceObject::Getter& reqGetProductionNumber(); 220 | virtual DeviceObject::Getter& reqGetProductionDate(); 221 | virtual DeviceObject::Getter& reqGetPowerSavingOperationSetting(); 222 | virtual DeviceObject::Getter& reqGetRemoteControlSetting(); 223 | virtual DeviceObject::Getter& reqGetCurrentTimeSetting(); 224 | virtual DeviceObject::Getter& reqGetCurrentDateSetting(); 225 | virtual DeviceObject::Getter& reqGetPowerLimitSetting(); 226 | virtual DeviceObject::Getter& reqGetCumulativeOperatingTime(); 227 | virtual DeviceObject::Getter& reqGetStatusChangeAnnouncementPropertyMap(); 228 | virtual DeviceObject::Getter& reqGetSetPropertyMap(); 229 | virtual DeviceObject::Getter& reqGetGetPropertyMap(); 230 | }; 231 | 232 | class Informer : public EchoObject::Informer { 233 | public: 234 | Informer(unsigned short echoClassCode, unsigned char echoInstanceCode 235 | , std::string dstEchoAddress, bool isSelfObject); 236 | virtual ~Informer(); 237 | virtual DeviceObject::Informer& reqInformProperty(unsigned char epc); 238 | 239 | virtual DeviceObject::Informer& reqInformOperationStatus(); 240 | virtual DeviceObject::Informer& reqInformInstallationLocation(); 241 | virtual DeviceObject::Informer& reqInformStandardVersionInformation(); 242 | virtual DeviceObject::Informer& reqInformIdentificationNumber(); 243 | virtual DeviceObject::Informer& reqInformMeasuredInstantaneousPowerConsumption(); 244 | virtual DeviceObject::Informer& reqInformMeasuredCumulativePowerConsumption(); 245 | virtual DeviceObject::Informer& reqInformManufacturersFaultCode(); 246 | virtual DeviceObject::Informer& reqInformCurrentLimitSetting(); 247 | virtual DeviceObject::Informer& reqInformFaultStatus(); 248 | virtual DeviceObject::Informer& reqInformFaultDescription(); 249 | virtual DeviceObject::Informer& reqInformManufacturerCode(); 250 | virtual DeviceObject::Informer& reqInformBusinessFacilityCode(); 251 | virtual DeviceObject::Informer& reqInformProductCode(); 252 | virtual DeviceObject::Informer& reqInformProductionNumber(); 253 | virtual DeviceObject::Informer& reqInformProductionDate(); 254 | virtual DeviceObject::Informer& reqInformPowerSavingOperationSetting(); 255 | virtual DeviceObject::Informer& reqInformRemoteControlSetting(); 256 | virtual DeviceObject::Informer& reqInformCurrentTimeSetting(); 257 | virtual DeviceObject::Informer& reqInformCurrentDateSetting(); 258 | virtual DeviceObject::Informer& reqInformPowerLimitSetting(); 259 | virtual DeviceObject::Informer& reqInformCumulativeOperatingTime(); 260 | virtual DeviceObject::Informer& reqInformStatusChangeAnnouncementPropertyMap(); 261 | virtual DeviceObject::Informer& reqInformSetPropertyMap(); 262 | virtual DeviceObject::Informer& reqInformGetPropertyMap(); 263 | 264 | }; 265 | 266 | class InformerC : public EchoObject::InformerC { 267 | public: 268 | InformerC(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 269 | , std::string dstEchoAddress); 270 | virtual ~InformerC(); 271 | virtual DeviceObject::InformerC& reqInformProperty(unsigned char epc); 272 | }; 273 | 274 | }; 275 | 276 | class DeviceObject::Proxy : public DeviceObject { 277 | protected: 278 | unsigned short mEchoClassCode; 279 | public: 280 | Proxy(unsigned short echoClassCode, unsigned char echoInstanceCode); 281 | virtual ~Proxy(); 282 | 283 | virtual unsigned char getInstanceCode(); 284 | virtual unsigned short getEchoClassCode(); 285 | 286 | virtual std::shared_ptr > getOperationStatus(); 287 | virtual bool setInstallationLocation(std::vector& edt); 288 | virtual std::shared_ptr > getInstallationLocation(); 289 | virtual std::shared_ptr > getFaultStatus(); 290 | virtual std::shared_ptr > getManufacturerCode(); 291 | }; 292 | 293 | }; 294 | 295 | #endif /* DEVICEOBJECT_H_ */ 296 | -------------------------------------------------------------------------------- /src/openecho/Echo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Echo.cpp 3 | * 4 | * Created on: 2013/10/21 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "Echo.h" 9 | #include "OpenECHO.h" 10 | #include "HouseholdSolarPowerGeneration.h" 11 | #include "SmartElectricEnergyMeter.h" 12 | 13 | 14 | namespace sonycsl_openecho { 15 | 16 | std::shared_ptr Echo::sStorage; 17 | std::shared_ptr Echo::sSelfNode; 18 | std::map > Echo::sOtherNodes; 19 | Echo::EventListenerDelegate Echo::sEventListenerDelegate; 20 | 21 | Echo::Echo() { 22 | // TODO Auto-generated constructor stub 23 | 24 | } 25 | 26 | Echo::~Echo() { 27 | // TODO Auto-generated destructor stub 28 | } 29 | 30 | std::shared_ptr Echo::start( 31 | std::shared_ptr profile, 32 | std::vector > devices) { 33 | if(sStorage.get() == nullptr) { 34 | sStorage = std::shared_ptr(new EchoStorage()); 35 | } 36 | 37 | sSelfNode = std::shared_ptr(new EchoNode(profile, devices)); 38 | sSelfNode.get()->getNodeProfile().get()->setNode(sSelfNode); 39 | int devicesSize = devices.size(); 40 | for(int i = 0; i < devicesSize; i++) { 41 | devices.at(i).get()->setNode(sSelfNode); 42 | } 43 | 44 | 45 | EchoSocket::openSocket(); 46 | EchoSocket::startReceiverThread(); 47 | //Echo::getEventListener().onNewNode(sSelfNode); 48 | //Echo::getEventListener().onFoundNode(sSelfNode); 49 | //sSelfNode.get()->onNewNode(); 50 | //sSelfNode.get()->onFoundNode(); 51 | sSelfNode.get()->onNew(sSelfNode); 52 | sSelfNode.get()->onFound(sSelfNode); 53 | 54 | sSelfNode.get()->getNodeProfile().get()->onNew(sSelfNode.get()->getNodeProfile()); 55 | sSelfNode.get()->getNodeProfile().get()->onFound(sSelfNode.get()->getNodeProfile()); 56 | 57 | for(int i = 0; i < devicesSize; i++) { 58 | devices.at(i).get()->onNew(devices.at(i)); 59 | devices.at(i).get()->onFound(devices.at(i)); 60 | 61 | } 62 | 63 | profile.get()->inform().reqInformInstanceListNotification().send(); 64 | 65 | return sSelfNode; 66 | } 67 | 68 | void Echo::restart() { 69 | } 70 | 71 | void Echo::stop() { 72 | } 73 | 74 | bool Echo::isStarted() { 75 | return true; 76 | } 77 | 78 | void Echo::setStorage(std::shared_ptr storage) { 79 | sStorage = storage; 80 | } 81 | 82 | std::shared_ptr Echo::getStorage() { 83 | return sStorage; 84 | } 85 | 86 | Echo::EventListener& Echo::getEventListenerDelegate() { 87 | return sEventListenerDelegate; 88 | } 89 | 90 | std::shared_ptr Echo::getSelfNode() { 91 | return sSelfNode; 92 | } 93 | 94 | std::vector > Echo::getNodes() { 95 | std::vector > nodes; 96 | nodes.push_back(sSelfNode); 97 | for(std::map >::iterator itr = sOtherNodes.begin(); 98 | itr != sOtherNodes.end(); itr++) { 99 | nodes.push_back(itr->second); 100 | } 101 | return nodes; 102 | } 103 | 104 | std::shared_ptr Echo::getNode(std::string address) { 105 | if(EchoSocket::SELF_ADDRESS == address) { 106 | return sSelfNode; 107 | } 108 | if (sOtherNodes.find(address) == sOtherNodes.end()) { 109 | std::shared_ptr node; 110 | return node; 111 | } else { 112 | return sOtherNodes.at(address); 113 | } 114 | } 115 | 116 | bool Echo::containsNode(std::string address) { 117 | std::shared_ptr node = getNode(address); 118 | if(node.get() == NULL) { 119 | return false; 120 | } 121 | return true; 122 | } 123 | 124 | std::shared_ptr Echo::addOtherNode(std::string address) { 125 | std::shared_ptr node(new EchoNode(address)); 126 | node.get()->getNodeProfile().get()->setNode(node); 127 | sOtherNodes.insert(std::map >::value_type(address, node)); 128 | return node; 129 | } 130 | 131 | void Echo::addEventListener( 132 | std::shared_ptr eventListener) { 133 | sEventListenerDelegate.addEventListener(eventListener); 134 | } 135 | 136 | void Echo::removeEventListener( 137 | std::shared_ptr eventListener) { 138 | sEventListenerDelegate.removeEventListener(eventListener); 139 | } 140 | 141 | void Echo::removeOtherNode(std::string address) { 142 | if (sOtherNodes.find(address) == sOtherNodes.end()) { 143 | //std::shared_ptr node; 144 | //return node; 145 | } else { 146 | //return mOtherNodes.at(address); 147 | sOtherNodes.erase(address); 148 | } 149 | } 150 | 151 | void Echo::EventListener::onNewNode(std::shared_ptr node) { 152 | } 153 | 154 | void Echo::EventListener::onNewHouseholdSolarPowerGeneration(std::shared_ptr device) 155 | { 156 | } 157 | void Echo::EventListener::onNewSmartElectricEnergyMeter(std::shared_ptr device) 158 | { 159 | } 160 | 161 | void Echo::EventListener::onFoundNode(std::shared_ptr node) { 162 | } 163 | 164 | void Echo::EventListener::onNewEchoObject(std::shared_ptr eoj) { 165 | } 166 | 167 | void Echo::EventListener::onFoundEchoObject(std::shared_ptr eoj) { 168 | } 169 | 170 | void Echo::EventListener::onNewNodeProfile( 171 | std::shared_ptr profile) { 172 | } 173 | 174 | Echo::EventListener::EventListener() { 175 | } 176 | 177 | Echo::EventListener::~EventListener() { 178 | } 179 | 180 | void Echo::EventListener::onNewDeviceObject(std::shared_ptr device) { 181 | } 182 | 183 | 184 | 185 | 186 | void Echo::EventListener::onSendFrame(EchoFrame& frame) { 187 | } 188 | 189 | void Echo::EventListener::onReceiveFrame(EchoFrame& frame) { 190 | } 191 | 192 | void Echo::EventListenerDelegate::addEventListener( 193 | std::shared_ptr eventListener) { 194 | mEventListeners.push_back(eventListener); 195 | 196 | } 197 | 198 | void Echo::EventListenerDelegate::removeEventListener( 199 | std::shared_ptr eventListener) { 200 | std::list >::iterator it = mEventListeners.begin(); 201 | while( it != mEventListeners.end() ) { 202 | if((*it).get() == eventListener.get()) { 203 | mEventListeners.erase(it); 204 | break; 205 | } 206 | ++it; 207 | } 208 | } 209 | 210 | void Echo::EventListenerDelegate::onNewNode(std::shared_ptr node) { 211 | std::list >::iterator it = mEventListeners.begin(); 212 | while( it != mEventListeners.end() ) { 213 | (*it).get()->onNewNode(node); 214 | ++it; 215 | } 216 | } 217 | 218 | 219 | void Echo::EventListenerDelegate::onFoundNode(std::shared_ptr node) { 220 | std::list >::iterator it = mEventListeners.begin(); 221 | while( it != mEventListeners.end() ) { 222 | (*it).get()->onFoundNode(node); 223 | ++it; 224 | } 225 | } 226 | 227 | void Echo::EventListenerDelegate::onNewEchoObject( 228 | std::shared_ptr eoj) { 229 | std::list >::iterator it = mEventListeners.begin(); 230 | while( it != mEventListeners.end() ) { 231 | (*it).get()->onNewEchoObject(eoj); 232 | ++it; 233 | } 234 | } 235 | 236 | void Echo::EventListenerDelegate::onFoundEchoObject( 237 | std::shared_ptr eoj) { 238 | std::list >::iterator it = mEventListeners.begin(); 239 | while( it != mEventListeners.end() ) { 240 | (*it).get()->onFoundEchoObject(eoj); 241 | ++it; 242 | } 243 | } 244 | 245 | void Echo::EventListenerDelegate::onNewNodeProfile( 246 | std::shared_ptr profile) { 247 | std::list >::iterator it = mEventListeners.begin(); 248 | while( it != mEventListeners.end() ) { 249 | (*it).get()->onNewNodeProfile(profile); 250 | ++it; 251 | } 252 | } 253 | 254 | void Echo::EventListenerDelegate::onNewDeviceObject( 255 | std::shared_ptr device) { 256 | std::list >::iterator it = mEventListeners.begin(); 257 | while( it != mEventListeners.end() ) { 258 | (*it).get()->onNewDeviceObject(device); 259 | ++it; 260 | } 261 | } 262 | 263 | void Echo::EventListenerDelegate::onNewHouseholdSolarPowerGeneration(std::shared_ptr device){ 264 | std::list>::iterator it = mEventListeners.begin(); 265 | while(it != mEventListeners.end()){ 266 | (*it).get()->onNewHouseholdSolarPowerGeneration(device); 267 | ++it; 268 | } 269 | } 270 | void Echo::EventListenerDelegate::onNewSmartElectricEnergyMeter(std::shared_ptr device){ 271 | std::list>::iterator it = mEventListeners.begin(); 272 | while(it != mEventListeners.end()){ 273 | (*it).get()->onNewSmartElectricEnergyMeter(device); 274 | ++it; 275 | } 276 | } 277 | 278 | void Echo::EventListenerDelegate::onSendFrame(EchoFrame& frame) { 279 | 280 | std::list >::iterator it = mEventListeners.begin(); 281 | while( it != mEventListeners.end() ) { 282 | (*it).get()->onSendFrame(frame); 283 | ++it; 284 | } 285 | } 286 | 287 | void Echo::EventListenerDelegate::onReceiveFrame(EchoFrame& frame) { 288 | 289 | std::list >::iterator it = mEventListeners.begin(); 290 | while( it != mEventListeners.end() ) { 291 | (*it).get()->onReceiveFrame(frame); 292 | ++it; 293 | } 294 | } 295 | 296 | void Echo::Logger::onNewNode(std::shared_ptr node) { 297 | std::cout << "[onNewNode]address:" << node.get()->getAddress() << std::endl; 298 | } 299 | 300 | void Echo::Logger::onFoundNode(std::shared_ptr node) { 301 | std::cout << "[onFoundNode]address:" << node.get()->getAddress() << std::endl; 302 | } 303 | 304 | void Echo::Logger::onNewEchoObject(std::shared_ptr eoj) { 305 | std::cout << "[onNewEchoObject]address:" << eoj.get()->getNode().get()->getAddress() << ",echo_class_code:" 306 | << std::hex << eoj.get()->getEchoClassCode() << ",instance_code:" 307 | << std::hex << (int)(eoj.get()->getInstanceCode()) << std::endl; 308 | } 309 | 310 | void Echo::Logger::onFoundEchoObject(std::shared_ptr eoj) { 311 | std::cout << "[onFoundEchoObject]address:" << eoj.get()->getNode().get()->getAddress() << ",echo_class_code:" 312 | << std::hex << eoj.get()->getEchoClassCode() << ",instance_code:" 313 | << std::hex << (int)(eoj.get()->getInstanceCode()) << std::endl; 314 | } 315 | 316 | void Echo::Logger::onSendFrame(EchoFrame& frame) { 317 | 318 | std::vector byteArray = frame.getFrameByteArray(); 319 | int size = byteArray.size(); 320 | unsigned char buffer[size]; 321 | for (int i = 0; i < size; i++) { 322 | buffer[i] = byteArray.at(i); 323 | } 324 | 325 | std::cout << "[onSendFrame]data:" << std::hex; 326 | for (int i = 0; i < size; i++) { 327 | std::cout << (int) (buffer[i]) << " "; 328 | } 329 | std::cout << ", to:" << frame.getDstEchoAddress() << std::endl; 330 | 331 | } 332 | 333 | void Echo::Logger::onReceiveFrame(EchoFrame& frame) { 334 | 335 | std::vector byteArray = frame.getFrameByteArray(); 336 | int size = byteArray.size(); 337 | unsigned char buffer[size]; 338 | for (int i = 0; i < size; i++) { 339 | buffer[i] = byteArray.at(i); 340 | } 341 | 342 | 343 | std::cout << "[onReceiveFrame]:data:" << std::hex; 344 | for (int i = 0; i < size; i++) { 345 | std::cout << (int) (buffer[i]) << " "; 346 | } 347 | std::cout << ", from:" << frame.getSrcEchoAddress() << std::endl; 348 | } 349 | }; 350 | 351 | -------------------------------------------------------------------------------- /src/openecho/Echo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Echo.h 3 | * 4 | * Created on: 2013/10/21 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHO_H_ 9 | #define ECHO_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "HouseholdSolarPowerGeneration.h" 17 | #include "SmartElectricEnergyMeter.h" 18 | 19 | 20 | namespace sonycsl_openecho { 21 | 22 | class EchoStorage; 23 | class EchoFrame; 24 | class EchoNode; 25 | class EchoObject; 26 | class NodeProfile; 27 | class DeviceObject; 28 | 29 | class Echo { 30 | public: 31 | class EventListener; 32 | private: 33 | class EventListenerDelegate; 34 | private: 35 | static std::shared_ptr sStorage; 36 | 37 | static std::shared_ptr sSelfNode; 38 | static std::map > sOtherNodes; 39 | 40 | static Echo::EventListenerDelegate sEventListenerDelegate; 41 | 42 | private: 43 | Echo(); 44 | ~Echo(); 45 | Echo(const Echo& rhs); 46 | Echo& operator=(const Echo& rhs); 47 | 48 | public: 49 | 50 | static std::shared_ptr start(std::shared_ptr profile, std::vector > devices); 51 | static void restart(); 52 | static void stop(); 53 | static bool isStarted(); 54 | 55 | static void setStorage(std::shared_ptr storage); 56 | static std::shared_ptr getStorage(); 57 | 58 | static Echo::EventListener& getEventListenerDelegate(); 59 | 60 | static void addEventListener(std::shared_ptr eventListener); 61 | static void removeEventListener(std::shared_ptr eventListener); 62 | 63 | static std::shared_ptr getSelfNode(); 64 | static std::vector > getNodes(); 65 | static std::shared_ptr getNode(std::string address); 66 | static bool containsNode(std::string address); 67 | static std::shared_ptr addOtherNode(std::string address); 68 | static void removeOtherNode(std::string address); 69 | 70 | public: 71 | class EventListener { 72 | public: 73 | EventListener(); 74 | virtual ~EventListener(); 75 | virtual void onNewNode(std::shared_ptr node); 76 | virtual void onNewHouseholdSolarPowerGeneration(std::shared_ptr device); 77 | virtual void onNewSmartElectricEnergyMeter(std::shared_ptr device); 78 | 79 | virtual void onFoundNode(std::shared_ptr node); 80 | virtual void onNewEchoObject(std::shared_ptr eoj); 81 | virtual void onFoundEchoObject(std::shared_ptr eoj); 82 | virtual void onNewNodeProfile(std::shared_ptr profile); 83 | virtual void onNewDeviceObject(std::shared_ptr device); 84 | 85 | // void onNewHouseholdSolarPowerGeneration(std::shared_ptr device); 86 | 87 | virtual void onSendFrame(EchoFrame& frame); 88 | virtual void onReceiveFrame(EchoFrame& frame); 89 | }; 90 | 91 | private: 92 | class EventListenerDelegate : public EventListener { 93 | private: 94 | std::list > mEventListeners; 95 | 96 | public: 97 | void addEventListener(std::shared_ptr eventListener); 98 | void removeEventListener(std::shared_ptr eventListener); 99 | 100 | virtual void onNewNode(std::shared_ptr node); 101 | virtual void onFoundNode(std::shared_ptr node); 102 | virtual void onNewHouseholdSolarPowerGeneration(std::shared_ptr device); 103 | virtual void onNewSmartElectricEnergyMeter(std::shared_ptr device); 104 | 105 | virtual void onNewEchoObject(std::shared_ptr eoj); 106 | virtual void onFoundEchoObject(std::shared_ptr eoj); 107 | virtual void onNewNodeProfile(std::shared_ptr profile); 108 | virtual void onNewDeviceObject(std::shared_ptr device); 109 | 110 | 111 | virtual void onSendFrame(EchoFrame& frame); 112 | virtual void onReceiveFrame(EchoFrame& frame); 113 | 114 | }; 115 | 116 | public: 117 | class Logger : public EventListener { 118 | 119 | virtual void onNewNode(std::shared_ptr node); 120 | virtual void onFoundNode(std::shared_ptr node); 121 | virtual void onNewEchoObject(std::shared_ptr eoj); 122 | virtual void onFoundEchoObject(std::shared_ptr eoj); 123 | virtual void onSendFrame(EchoFrame& frame); 124 | virtual void onReceiveFrame(EchoFrame& frame); 125 | }; 126 | }; 127 | 128 | }; 129 | 130 | #endif /* ECHO_H_ */ 131 | -------------------------------------------------------------------------------- /src/openecho/EchoFrame.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoFrame.cpp 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoFrame.h" 9 | #include "OpenECHO.h" 10 | 11 | 12 | namespace sonycsl_openecho { 13 | 14 | const unsigned char EchoFrame::EHD1 = 0x10; 15 | const unsigned char EchoFrame::EHD2 = 0x81; 16 | 17 | EchoFrame::EchoFrame(const unsigned short srcEchoClassCode, 18 | const unsigned char srcEchoInstanceCode, 19 | const unsigned short dstEchoClassCode, 20 | const unsigned char dstEchoInstanceCode, 21 | const std::string dstEchoAddress, const unsigned char esv) { 22 | mSrcEchoClassCode = srcEchoClassCode; 23 | mSrcEchoInstanceCode = srcEchoInstanceCode; 24 | mSrcEchoAddress = EchoSocket::SELF_ADDRESS; 25 | mDstEchoClassCode = dstEchoClassCode; 26 | mDstEchoInstanceCode = dstEchoInstanceCode; 27 | mDstEchoAddress = dstEchoAddress; 28 | mESV = esv; 29 | 30 | mTID = 0; 31 | } 32 | 33 | EchoFrame::EchoFrame(const std::string srcEchoAddress, 34 | const std::vector& frame) { 35 | mSrcEchoAddress = srcEchoAddress; 36 | mDstEchoAddress = EchoSocket::SELF_ADDRESS; 37 | if(frame.at(0) != EHD1) return; 38 | if(frame.at(1) != EHD2) return; 39 | mTID = (frame.at(2) << 8) & 0xFF00; 40 | mTID += frame.at(3) & 0xFF; 41 | mSrcEchoClassCode = (frame.at(4) << 8) & 0xFF00; 42 | mSrcEchoClassCode += frame.at(5) & 0xFF; 43 | mSrcEchoInstanceCode = frame.at(6); 44 | mDstEchoClassCode = (frame.at(7) << 8) & 0xFF00; 45 | mDstEchoClassCode += frame.at(8) & 0xFF; 46 | mDstEchoInstanceCode = frame.at(9); 47 | mESV = frame.at(10); 48 | int propertyListSize = frame.at(11); 49 | for(int i = 0, pos = 12; i < propertyListSize; i++) { 50 | unsigned char epc = frame.at(pos); 51 | unsigned char pdc = frame.at(pos+1); 52 | pos += 2; 53 | if(pdc == 0) { 54 | mPropertyList.push_back(EchoProperty(epc)); 55 | } else { 56 | std::vector edt; 57 | for(unsigned char j = 0; j < pdc; j++) { 58 | edt.push_back(frame.at(pos)); 59 | pos += 1; 60 | } 61 | mPropertyList.push_back(EchoProperty(epc, edt)); 62 | } 63 | } 64 | 65 | } 66 | 67 | EchoFrame::~EchoFrame() { 68 | // TODO Auto-generated destructor stub 69 | } 70 | 71 | unsigned short EchoFrame::getTID() { 72 | return mTID; 73 | } 74 | 75 | void EchoFrame::setTID(unsigned short tid) { 76 | mTID = tid; 77 | } 78 | 79 | unsigned char EchoFrame::getESV() { 80 | return mESV; 81 | } 82 | 83 | unsigned short EchoFrame::getSrcEchoClassCode() { 84 | return mSrcEchoClassCode; 85 | } 86 | 87 | unsigned char EchoFrame::getSrcEchoInstanceCode() { 88 | return mSrcEchoInstanceCode; 89 | } 90 | 91 | std::string EchoFrame::getSrcEchoAddress() { 92 | return mSrcEchoAddress; 93 | } 94 | 95 | unsigned short EchoFrame::getDstEchoClassCode() { 96 | return mDstEchoClassCode; 97 | } 98 | 99 | 100 | void EchoFrame::setDstEchoInstanceCode(const unsigned char echoInstanceCode) { 101 | mDstEchoInstanceCode = echoInstanceCode; 102 | } 103 | 104 | unsigned char EchoFrame::getDstEchoInstanceCode() { 105 | return mDstEchoInstanceCode; 106 | } 107 | 108 | std::string EchoFrame::getDstEchoAddress() const { 109 | return mDstEchoAddress; 110 | } 111 | 112 | void EchoFrame::setDstEchoAddress(const std::string dstEchoAddress) { 113 | mDstEchoAddress = dstEchoAddress; 114 | } 115 | 116 | std::vector EchoFrame::getPropertyList() { 117 | return mPropertyList; 118 | } 119 | 120 | std::vector EchoFrame::getFrameByteArray() const { 121 | std::vector frame; 122 | int propertyListSize = mPropertyList.size(); 123 | if(propertyListSize > 255) { 124 | propertyListSize = 255; 125 | } 126 | int packetSize = 12; 127 | for(int i = 0; i < propertyListSize; i++) { 128 | packetSize += mPropertyList.at(i).size(); 129 | if(packetSize > EchoUDPProtocol::UDP_MAX_PACKET_SIZE) { 130 | propertyListSize = i - 1; 131 | break; 132 | } 133 | } 134 | 135 | frame.push_back(EHD1); 136 | frame.push_back(EHD2); 137 | frame.push_back(((mTID >> 8) & 0xff)); 138 | frame.push_back((mTID & 0xff)); 139 | frame.push_back(((mSrcEchoClassCode >> 8) & 0xff)); 140 | frame.push_back((mSrcEchoClassCode & 0xff)); 141 | frame.push_back(mSrcEchoInstanceCode); 142 | frame.push_back(((mDstEchoClassCode >> 8) & 0xff)); 143 | frame.push_back((mDstEchoClassCode & 0xff)); 144 | frame.push_back(mDstEchoInstanceCode); 145 | frame.push_back(mESV); 146 | frame.push_back(propertyListSize); 147 | for(int pidx = 0; pidx < mPropertyList.size(); pidx++) { 148 | EchoProperty p = mPropertyList.at(pidx); 149 | frame.push_back(p.epc); 150 | frame.push_back(p.pdc); 151 | int size = p.pdc & 0xFF; 152 | for(int i = 0; i < size; i++) { 153 | frame.push_back(p.edt.at(i)); 154 | } 155 | } 156 | if(mESV == ESV_SET_GET_SNA) { 157 | frame.push_back(0); 158 | } 159 | return frame; 160 | } 161 | 162 | void EchoFrame::addPropertyForResponse(const unsigned char epc) { 163 | EchoProperty property(epc); 164 | addPropertyForResponse(property); 165 | } 166 | 167 | void EchoFrame::addPropertyForResponse(const unsigned char epc, 168 | const std::vector& edt) { 169 | EchoProperty property(epc, edt); 170 | addPropertyForResponse(property); 171 | } 172 | 173 | void EchoFrame::addPropertyForResponse(const EchoProperty& property) { 174 | mPropertyList.push_back(property); 175 | switch(mESV) { 176 | case ESV_SET_NO_RES: case ESV_SETI_SNA: 177 | if(property.pdc != 0) { 178 | mESV = ESV_SETI_SNA; 179 | } 180 | break; 181 | case ESV_SET_RES: case ESV_SETC_SNA: 182 | if(property.pdc != 0) { 183 | mESV = ESV_SETC_SNA; 184 | } 185 | break; 186 | case ESV_GET_RES: case ESV_GET_SNA: 187 | if(property.pdc == 0) { 188 | mESV = ESV_GET_SNA; 189 | } 190 | break; 191 | case ESV_INF: case ESV_INF_SNA: 192 | if(property.pdc == 0) { 193 | mESV = ESV_INF_SNA; 194 | } 195 | break; 196 | } 197 | } 198 | 199 | EchoFrame::EchoFrame() { 200 | } 201 | 202 | void EchoFrame::addProperty(const EchoProperty& property) { 203 | mPropertyList.push_back(property); 204 | } 205 | 206 | }; 207 | -------------------------------------------------------------------------------- /src/openecho/EchoFrame.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoFrame.h 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOFRAME_H_ 9 | #define ECHOFRAME_H_ 10 | 11 | #include "OpenECHO.h" 12 | 13 | 14 | namespace sonycsl_openecho { 15 | 16 | 17 | class EchoProperty; 18 | 19 | class EchoFrame { 20 | protected: 21 | static const unsigned char EHD1; 22 | static const unsigned char EHD2; 23 | 24 | public: 25 | static const int MIN_FRAME_SIZE = 12; 26 | 27 | static const unsigned char ESV_SETI = 0x60; 28 | static const unsigned char ESV_SETC = 0x61; 29 | static const unsigned char ESV_GET = 0x62; 30 | static const unsigned char ESV_INF_REQ = 0x63; 31 | static const unsigned char ESV_SET_RES = 0x71; 32 | static const unsigned char ESV_GET_RES = 0x72; 33 | static const unsigned char ESV_INF = 0x73; 34 | static const unsigned char ESV_INFC = 0x74; 35 | static const unsigned char ESV_INFC_RES = 0x7A; 36 | static const unsigned char ESV_SETI_SNA = 0x50; 37 | static const unsigned char ESV_SETC_SNA = 0x51; 38 | static const unsigned char ESV_GET_SNA = 0x52; 39 | static const unsigned char ESV_INF_SNA = 0x53; 40 | 41 | static const unsigned char ESV_SET_NO_RES = 0x70; 42 | 43 | static const unsigned char ESV_SET_GET = 0x6E; 44 | static const unsigned char ESV_SET_GET_RES = 0x7E; 45 | static const unsigned char ESV_SET_GET_SNA = 0x5E; 46 | 47 | private: 48 | unsigned short mTID; 49 | unsigned char mESV; 50 | 51 | unsigned short mSrcEchoClassCode; 52 | unsigned char mSrcEchoInstanceCode; 53 | std::string mSrcEchoAddress; 54 | unsigned short mDstEchoClassCode; 55 | unsigned char mDstEchoInstanceCode; 56 | std::string mDstEchoAddress; 57 | 58 | std::vector mPropertyList; 59 | public: 60 | EchoFrame(); 61 | EchoFrame(const unsigned short srcEchoClassCode, const unsigned char srcEchoInstanceCode 62 | , const unsigned short dstEchoClassCode, const unsigned char dstEchoInstanceCode 63 | , const std::string dstEchoAddress, const unsigned char esv); // send 64 | EchoFrame(const std::string srcEchoAddress, const std::vector& frame); // receive 65 | virtual ~EchoFrame(); 66 | 67 | unsigned short getTID(); 68 | void setTID(unsigned short tid); 69 | unsigned char getESV(); 70 | unsigned short getSrcEchoClassCode(); 71 | unsigned char getSrcEchoInstanceCode(); 72 | std::string getSrcEchoAddress(); 73 | unsigned short getDstEchoClassCode(); 74 | void setDstEchoInstanceCode(const unsigned char echoInstanceCode); 75 | unsigned char getDstEchoInstanceCode(); 76 | void setDstEchoAddress(const std::string dstEchoAddress); 77 | std::string getDstEchoAddress() const; 78 | std::vector getPropertyList(); 79 | std::vector getFrameByteArray() const; 80 | void addPropertyForResponse(const unsigned char epc); 81 | void addPropertyForResponse(const unsigned char epc, const std::vector& edt); 82 | void addPropertyForResponse(const EchoProperty& property); 83 | void addProperty(const EchoProperty& property); 84 | }; 85 | 86 | }; 87 | 88 | #endif /* ECHOFRAME_H_ */ 89 | -------------------------------------------------------------------------------- /src/openecho/EchoNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoNode.cpp 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoNode.h" 9 | #include "OpenECHO.h" 10 | 11 | 12 | namespace sonycsl_openecho { 13 | 14 | EchoNode::EchoNode(std::shared_ptr nodeProfile, 15 | std::vector > devices) { 16 | // self node 17 | mAddress = EchoSocket::SELF_ADDRESS; 18 | mNodeProfile = nodeProfile; 19 | int devicesSize = devices.size(); 20 | for(int i = 0; i < devicesSize; i++) { 21 | //devices.at(i).get()->allocateSelfDeviceInstanceCode(); 22 | //mDevices.push_back(devices.at(i)); 23 | //addDevice(devices.at(i)); 24 | if(isSelfNode()) { 25 | devices.at(i).get()->allocateSelfDeviceInstanceCode(); 26 | mDevices.push_back(devices.at(i)); 27 | } 28 | } 29 | } 30 | 31 | EchoNode::EchoNode(std::string address) { 32 | // other node 33 | mAddress = address; 34 | mNodeProfile = std::shared_ptr(new NodeProfile::Proxy()); 35 | } 36 | 37 | 38 | EchoNode::~EchoNode() { 39 | // TODO Auto-generated destructor stub 40 | } 41 | 42 | bool EchoNode::isProxy() { 43 | return (mAddress != EchoSocket::SELF_ADDRESS); 44 | } 45 | 46 | bool EchoNode::isSelfNode() { 47 | return (mAddress == EchoSocket::SELF_ADDRESS); 48 | } 49 | 50 | std::shared_ptr EchoNode::getNodeProfile() { 51 | return mNodeProfile; 52 | } 53 | 54 | std::string EchoNode::getAddress() { 55 | return mAddress; 56 | } 57 | 58 | std::shared_ptr EchoNode::addOtherDevice(unsigned short echoClassCode, 59 | unsigned char echoInstanceCode) { 60 | std::shared_ptr device(newOtherDevice(echoClassCode, echoInstanceCode)); 61 | addDevice(device); 62 | return device; 63 | } 64 | 65 | 66 | void EchoNode::addDevice(std::shared_ptr device) { 67 | if(device.get() == nullptr) { 68 | return; 69 | } 70 | if(device.get()->getNode().get() == this) { 71 | return; 72 | } 73 | 74 | if(isSelfNode()) { 75 | device.get()->allocateSelfDeviceInstanceCode(); 76 | device.get()->setNode(Echo::getSelfNode()); 77 | mDevices.push_back(device); 78 | device.get()->onNew(device); 79 | device.get()->onFound(device); 80 | } else { 81 | mDevices.push_back(device); 82 | } 83 | 84 | } 85 | void EchoNode::removeDevice(unsigned short echoClassCode, 86 | unsigned char echoInstanceCode) { 87 | int listSize = mDevices.size(); 88 | for(int i = 0; i < listSize; i++) { 89 | if(mDevices.at(i).get()->getEchoClassCode() == echoClassCode 90 | && mDevices.at(i).get()->getInstanceCode() == echoInstanceCode) { 91 | mDevices.erase(mDevices.begin() + i); 92 | 93 | if(isSelfNode()) { 94 | 95 | } 96 | break; 97 | } 98 | } 99 | } 100 | 101 | bool EchoNode::containsDevice(unsigned short echoClassCode, 102 | unsigned char echoInstanceCode) { 103 | 104 | int listSize = mDevices.size(); 105 | for(int i = 0; i < listSize; i++) { 106 | if(mDevices.at(i).get()->getEchoClassCode() == echoClassCode 107 | && mDevices.at(i).get()->getInstanceCode() == echoInstanceCode) { 108 | 109 | return true; 110 | } 111 | } 112 | return false; 113 | } 114 | 115 | std::shared_ptr EchoNode::getInstance( 116 | unsigned short echoClassCode, unsigned char echoInstanceCode) { 117 | if(echoClassCode == mNodeProfile.get()->getEchoClassCode() 118 | && echoInstanceCode == mNodeProfile.get()->getInstanceCode()) { 119 | return mNodeProfile; 120 | } 121 | 122 | int listSize = mDevices.size(); 123 | for(int i = 0; i < listSize; i++) { 124 | if(mDevices.at(i).get()->getEchoClassCode() == echoClassCode 125 | && mDevices.at(i).get()->getInstanceCode() == echoInstanceCode) { 126 | return mDevices.at(i); 127 | } 128 | } 129 | std::shared_ptr eoj; 130 | return eoj; 131 | } 132 | 133 | std::vector > EchoNode::getDevices() { 134 | 135 | return mDevices; 136 | } 137 | 138 | 139 | std::vector > EchoNode::getDevices( 140 | unsigned short echoClassCode) { 141 | 142 | std::vector > ret; 143 | 144 | int listSize = mDevices.size(); 145 | for(int i = 0; i < listSize; i++) { 146 | if(mDevices.at(i).get()->getEchoClassCode() == echoClassCode) { 147 | ret.push_back(mDevices.at(i)); 148 | } 149 | } 150 | return ret; 151 | } 152 | 153 | void EchoNode::onNew(std::shared_ptr node) { 154 | Echo::getEventListenerDelegate().onNewNode(node); 155 | } 156 | 157 | void EchoNode::onFound(std::shared_ptr node) { 158 | Echo::getEventListenerDelegate().onFoundNode(node); 159 | } 160 | 161 | DeviceObject* EchoNode::newOtherDevice(unsigned short echoClassCode, 162 | unsigned char echoInstanceCode) { 163 | //return 0; 164 | DeviceObject* device = new DeviceObject::Proxy(echoClassCode, echoInstanceCode); 165 | 166 | return device; 167 | } 168 | 169 | }; 170 | 171 | -------------------------------------------------------------------------------- /src/openecho/EchoNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoNode.h 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHONODE_H_ 9 | #define ECHONODE_H_ 10 | 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | 18 | namespace sonycsl_openecho { 19 | 20 | 21 | class EchoObject; 22 | class NodeProfile; 23 | class DeviceObject; 24 | 25 | 26 | class EchoNode { 27 | private: 28 | std::shared_ptr mNodeProfile; 29 | std::vector > mDevices; 30 | std::string mAddress; 31 | //protected: 32 | // int mNodeIndex; 33 | public: 34 | EchoNode(std::shared_ptr nodeProfile, std::vector > devices); 35 | EchoNode(std::string address); 36 | virtual ~EchoNode(); 37 | 38 | private: 39 | void operator =(const EchoNode& src); // non-copyable 40 | EchoNode(const EchoNode& src); // non-copyable 41 | 42 | public: 43 | void onNew(std::shared_ptr node); 44 | void onFound(std::shared_ptr node); 45 | bool isProxy(); 46 | bool isSelfNode(); 47 | std::shared_ptr getNodeProfile(); 48 | std::string getAddress(); 49 | std::shared_ptr addOtherDevice(unsigned short echoClassCode, unsigned char echoInstanceCode); 50 | void addDevice(std::shared_ptr device); 51 | void removeDevice(unsigned short echoClassCode, unsigned char echoInstanceCode); 52 | bool containsDevice(unsigned short echoClassCode, unsigned char echoInstanceCode); 53 | std::shared_ptr getInstance(unsigned short echoClassCode, unsigned char echoInstanceCode); 54 | std::vector > getDevices(); 55 | std::vector > getDevices(unsigned short echoClassCode); 56 | private: 57 | static DeviceObject* newOtherDevice(unsigned short echoClassCode, unsigned char echoInstanceCode); 58 | }; 59 | 60 | }; 61 | 62 | #endif /* ECHONODE_H_ */ 63 | -------------------------------------------------------------------------------- /src/openecho/EchoObject.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoObject.cpp 3 | * 4 | * Created on: 2013/10/23 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoObject.h" 9 | #include "OpenECHO.h" 10 | 11 | 12 | namespace sonycsl_openecho { 13 | 14 | EchoObject::EchoObject() { 15 | // TODO Auto-generated constructor stub 16 | 17 | //mReceiver = NULL; 18 | mAnnouncementProperties = std::shared_ptr >(new std::set()); 19 | mSetProperties = std::shared_ptr >(new std::set()); 20 | mGetProperties = std::shared_ptr >(new std::set()); 21 | 22 | 23 | } 24 | 25 | EchoObject::~EchoObject() { 26 | // TODO Auto-generated destructor stub 27 | } 28 | 29 | void EchoObject::init() { 30 | setupPropertyMaps(); 31 | } 32 | 33 | unsigned char EchoObject::getClassGroupCode() { 34 | unsigned short code = getEchoClassCode(); 35 | return (unsigned char)((code >> 8) & 0xFF); 36 | } 37 | 38 | unsigned char EchoObject::getClassCode() { 39 | unsigned short code = getEchoClassCode(); 40 | return (unsigned char)(code & 0xFF); 41 | } 42 | 43 | long EchoObject::getEchoObjectCode() { 44 | return EchoUtils::convertToEchoObjectCode(getEchoClassCode(), getInstanceCode()); 45 | } 46 | 47 | 48 | void EchoObject::setNode(std::shared_ptr node) { 49 | mNode = node; 50 | 51 | //Echo::EventListener listener = Echo::getEventListener(); 52 | //std::shared_ptr eoj 53 | //listener.onNewEchoObject(this); 54 | } 55 | 56 | std::shared_ptr EchoObject::getNode() { 57 | return mNode; 58 | } 59 | 60 | void EchoObject::removeNode() { 61 | mNode = std::shared_ptr(); 62 | } 63 | 64 | 65 | bool EchoObject::isProxy() { 66 | std::shared_ptr node = getNode(); 67 | if(node.get() == NULL) { 68 | return true; 69 | } 70 | return node.get()->isProxy(); 71 | } 72 | 73 | bool EchoObject::isSelfObject() { 74 | std::shared_ptr node = getNode(); 75 | if(node.get() == NULL) { 76 | return false; 77 | } 78 | return node.get()->isSelfNode(); 79 | } 80 | 81 | bool EchoObject::setProperty(EchoProperty& property) { 82 | return false; 83 | } 84 | 85 | std::shared_ptr > EchoObject::getProperty(unsigned char epc) { 86 | return std::shared_ptr >(); 87 | } 88 | 89 | bool EchoObject::isValidProperty(EchoProperty& property) { 90 | return false; 91 | } 92 | 93 | void EchoObject::setReceiver(std::shared_ptr receiver) { 94 | mReceiver = receiver; 95 | } 96 | 97 | EchoFrame EchoObject::onReceiveRequest(EchoFrame& frame) { 98 | // receive response 99 | /* 100 | switch(frame.getESV()) { 101 | case EchoFrame::ESV_SET_RES: case EchoFrame::ESV_SETI_SNA: case EchoFrame::ESV_SETC_SNA: 102 | case EchoFrame::ESV_GET_RES: case EchoFrame::ESV_GET_SNA: 103 | case EchoFrame::ESV_INF: case EchoFrame::ESV_INF_SNA: 104 | case EchoFrame::ESV_INFC: 105 | case EchoFrame::ESV_INFC_RES: 106 | if(mReceiver.get() != NULL) { 107 | mReceiver.get()->receive(frame); 108 | } 109 | return; 110 | }*/ 111 | 112 | // receive request 113 | unsigned char esv; 114 | 115 | switch(frame.getESV()) { 116 | case EchoFrame::ESV_SETI: 117 | esv = EchoFrame::ESV_SET_NO_RES; 118 | break; 119 | case EchoFrame::ESV_SETC: 120 | esv = EchoFrame::ESV_SET_RES; 121 | break; 122 | case EchoFrame::ESV_GET: 123 | esv = EchoFrame::ESV_GET_RES; 124 | break; 125 | case EchoFrame::ESV_INF_REQ: 126 | esv = EchoFrame::ESV_INF; 127 | break; 128 | case EchoFrame::ESV_INFC: 129 | esv = EchoFrame::ESV_INFC_RES; 130 | break; 131 | case EchoFrame::ESV_SET_GET: 132 | esv = EchoFrame::ESV_SET_GET_SNA; 133 | break; 134 | } 135 | EchoFrame response(frame.getDstEchoClassCode(), frame.getDstEchoInstanceCode() 136 | , frame.getSrcEchoClassCode(), frame.getSrcEchoInstanceCode() 137 | , frame.getSrcEchoAddress(), esv); 138 | response.setTID(frame.getTID()); 139 | switch(frame.getESV()) { 140 | case EchoFrame::ESV_SETI: 141 | case EchoFrame::ESV_SETC: 142 | for(int pidx = 0; pidx < frame.getPropertyList().size(); pidx++) { 143 | EchoProperty p = frame.getPropertyList().at(pidx); 144 | onReceiveSetRequest(p, response); 145 | } 146 | break; 147 | case EchoFrame::ESV_GET: 148 | case EchoFrame::ESV_INF_REQ: 149 | for(int pidx = 0; pidx < frame.getPropertyList().size(); pidx++) { 150 | EchoProperty p = frame.getPropertyList().at(pidx); 151 | onReceiveGetRequest(p.epc, response); 152 | } 153 | break; 154 | case EchoFrame::ESV_INFC: 155 | for(int pidx = 0; pidx < frame.getPropertyList().size(); pidx++) { 156 | EchoProperty p = frame.getPropertyList().at(pidx); 157 | response.addPropertyForResponse(p.epc); 158 | } 159 | break; 160 | } 161 | /* 162 | switch(frame.getESV()) { 163 | case EchoFrame::ESV_SETI_SNA: 164 | case EchoFrame::ESV_SET_RES: case EchoFrame::ESV_SETC_SNA: 165 | case EchoFrame::ESV_GET_RES: case EchoFrame::ESV_GET_SNA: 166 | case EchoFrame::ESV_INF_SNA: 167 | case EchoFrame::ESV_INFC_RES: 168 | break; 169 | case EchoFrame::ESV_INF: 170 | //response.setDstEchoAddress(EchoSocket::MULTICAST_ADDRESS); 171 | break; 172 | case EchoFrame::ESV_SET_GET_SNA: 173 | //EchoSocket::sendUDPFrame(frame); 174 | break; 175 | }*/ 176 | return response; 177 | 178 | } 179 | 180 | EchoObject::Setter EchoObject::set() { 181 | return EchoObject::Setter(getEchoClassCode(), getInstanceCode() 182 | , getNode().get()->getAddress(), true); 183 | } 184 | 185 | EchoObject::Setter EchoObject::set(bool responseRequired) { 186 | return EchoObject::Setter(getEchoClassCode(), getInstanceCode() 187 | , getNode().get()->getAddress(), responseRequired); 188 | } 189 | 190 | EchoObject::Getter EchoObject::get() { 191 | return EchoObject::Getter(getEchoClassCode(), getInstanceCode() 192 | , getNode().get()->getAddress()); 193 | } 194 | 195 | EchoObject::Informer EchoObject::inform() { 196 | 197 | std::string address; 198 | if(isSelfObject()) { 199 | address = EchoSocket::MULTICAST_ADDRESS; 200 | } else { 201 | address = getNode().get()->getAddress(); 202 | } 203 | return EchoObject::Informer(getEchoClassCode(), getInstanceCode() 204 | , address, isSelfObject()); 205 | } 206 | 207 | void EchoObject::setupPropertyMaps() { 208 | } 209 | void EchoObject::onReceiveSetRequest(EchoProperty& property, 210 | EchoFrame& response) { 211 | bool success = false; 212 | if(mSetProperties.get()->find(property.epc) == mSetProperties.get()->end()) { 213 | // not contains 214 | success = false; 215 | } else { 216 | // contains 217 | bool valid = isValidProperty(property); 218 | // event listner 219 | if(valid) { 220 | success = setProperty(property); 221 | // event listner 222 | } else { 223 | success = false; 224 | } 225 | 226 | } 227 | if(success) { 228 | response.addPropertyForResponse(property.epc); 229 | } else { 230 | response.addPropertyForResponse(property.epc, property.edt); 231 | } 232 | 233 | } 234 | 235 | void EchoObject::addStatusChangeAnnouncementProperty(unsigned char epc) { 236 | mAnnouncementProperties.get()->insert(epc); 237 | } 238 | 239 | void EchoObject::removeStatusChangeAnnouncementProperty(unsigned char epc) { 240 | mAnnouncementProperties.get()->erase(epc); 241 | } 242 | 243 | void EchoObject::clearStatusChangeAnnouncementProperties() { 244 | mAnnouncementProperties.get()->clear(); 245 | } 246 | 247 | std::shared_ptr > EchoObject::getStatusChangeAnnouncementProperties() { 248 | return mAnnouncementProperties; 249 | } 250 | 251 | void EchoObject::addSetProperty(unsigned char epc) { 252 | mSetProperties.get()->insert(epc); 253 | } 254 | 255 | void EchoObject::removeSetProperty(unsigned char epc) { 256 | mSetProperties.get()->erase(epc); 257 | } 258 | 259 | void EchoObject::clearSetProperties() { 260 | mSetProperties.get()->clear(); 261 | } 262 | 263 | std::shared_ptr > EchoObject::getSetProperties() { 264 | return mSetProperties; 265 | } 266 | 267 | void EchoObject::addGetProperty(unsigned char epc) { 268 | mGetProperties.get()->insert(epc); 269 | } 270 | 271 | void EchoObject::removeGetProperty(unsigned char epc) { 272 | mGetProperties.get()->erase(epc); 273 | } 274 | 275 | void EchoObject::clearGetProperties() { 276 | mGetProperties.get()->clear(); 277 | } 278 | 279 | std::shared_ptr > EchoObject::getGetProperties() { 280 | return mGetProperties; 281 | } 282 | 283 | void EchoObject::onNew(std::shared_ptr eoj) { 284 | init(); 285 | Echo::getEventListenerDelegate().onNewEchoObject(eoj); 286 | } 287 | 288 | void EchoObject::onFound(std::shared_ptr eoj) { 289 | Echo::getEventListenerDelegate().onFoundEchoObject(eoj); 290 | } 291 | 292 | void EchoObject::onReceiveGetRequest(unsigned char epc, EchoFrame& response) { 293 | 294 | std::shared_ptr > edt; 295 | if(response.getESV() == EchoFrame::ESV_GET_RES 296 | || response.getESV() == EchoFrame::ESV_GET_SNA) { 297 | if(mGetProperties.get()->find(epc) == mGetProperties.get()->end()) { 298 | // not contains 299 | //edt = 0; 300 | } else { 301 | // contains 302 | edt = getProperty(epc); 303 | // event listner 304 | 305 | } 306 | 307 | } else { 308 | edt = getProperty(epc); 309 | // event listner 310 | 311 | } 312 | 313 | bool valid = false; 314 | if(edt.get() != nullptr) { 315 | EchoProperty property(epc, *(edt.get())); 316 | 317 | valid = isValidProperty(property); 318 | } 319 | //EchoProperty property(epc, edt); 320 | 321 | //bool valid = isValidProperty(property); 322 | // event listner 323 | if(valid) { 324 | response.addPropertyForResponse(epc, *(edt.get())); 325 | } else { 326 | response.addPropertyForResponse(epc); 327 | } 328 | 329 | } 330 | 331 | EchoObject::Informer EchoObject::inform(bool multicast) { 332 | std::string address; 333 | if(multicast) { 334 | address = EchoSocket::MULTICAST_ADDRESS; 335 | } else { 336 | address = getNode().get()->getAddress(); 337 | } 338 | return EchoObject::Informer(getEchoClassCode(), getInstanceCode() 339 | , address, isSelfObject()); 340 | } 341 | 342 | EchoObject::InformerC EchoObject::informC(std::string address) { 343 | return EchoObject::InformerC(getEchoClassCode(), getInstanceCode() 344 | , address); 345 | } 346 | 347 | EchoObject::Receiver::Receiver() { 348 | } 349 | 350 | EchoObject::Receiver::~Receiver() { 351 | } 352 | 353 | bool EchoObject::Receiver::onSetProperty(std::shared_ptr eoj, unsigned short tid, 354 | unsigned char esv, EchoProperty& property, bool success) { 355 | return false; 356 | } 357 | 358 | bool EchoObject::Receiver::onGetProperty(std::shared_ptr eoj, unsigned short tid, 359 | unsigned char esv, EchoProperty& property, bool success) { 360 | return false; 361 | } 362 | 363 | void EchoObject::Receiver::onReceive(std::shared_ptr eoj, EchoFrame& frame) { 364 | onReceiveFrame(eoj, frame); 365 | 366 | switch(frame.getESV()) { 367 | case EchoFrame::ESV_SET_RES: case EchoFrame::ESV_SETI_SNA: case EchoFrame::ESV_SETC_SNA: 368 | for(int pidx = 0; pidx < frame.getPropertyList().size(); pidx++) { 369 | EchoProperty p = frame.getPropertyList().at(pidx); 370 | onSetProperty(eoj, frame.getTID(), frame.getESV(), p, (p.pdc == 0)); 371 | } 372 | break; 373 | case EchoFrame::ESV_GET_RES: case EchoFrame::ESV_GET_SNA: 374 | case EchoFrame::ESV_INF: case EchoFrame::ESV_INF_SNA: 375 | case EchoFrame::ESV_INFC: 376 | for(int pidx = 0; pidx < frame.getPropertyList().size(); pidx++) { 377 | EchoProperty p = frame.getPropertyList().at(pidx); 378 | onGetProperty(eoj, frame.getTID(), frame.getESV(), p, (p.pdc != 0)); 379 | } 380 | break; 381 | case EchoFrame::ESV_INFC_RES: 382 | for(int pidx = 0; pidx < frame.getPropertyList().size(); pidx++) { 383 | EchoProperty p = frame.getPropertyList().at(pidx); 384 | onInformProperty(eoj, frame.getTID(), frame.getESV(), p); 385 | } 386 | break; 387 | } 388 | } 389 | 390 | void EchoObject::Receiver::onReceiveFrame(std::shared_ptr eoj, EchoFrame& frame) { 391 | } 392 | 393 | bool EchoObject::Receiver::onInformProperty(std::shared_ptr eoj, unsigned short tid, 394 | unsigned char esv, EchoProperty& property) { 395 | return false; 396 | } 397 | 398 | EchoObject::Sender::Sender(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 399 | , unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 400 | , std::string dstEchoAddress, unsigned char esv) 401 | : mSrcEchoClassCode(srcEchoClassCode), mSrcEchoInstanceCode(srcEchoInstanceCode) 402 | , mDstEchoClassCode(dstEchoClassCode), mDstEchoInstanceCode(dstEchoInstanceCode) 403 | , mDstEchoAddress(dstEchoAddress), mESV(esv) { 404 | } 405 | 406 | EchoObject::Sender::~Sender() { 407 | } 408 | 409 | void EchoObject::Sender::setSeoj(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode) { 410 | mSrcEchoClassCode = srcEchoClassCode; 411 | mSrcEchoInstanceCode = srcEchoInstanceCode; 412 | } 413 | 414 | void EchoObject::Sender::setDeoj(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 415 | , std::string dstEchoAddress) { 416 | mDstEchoClassCode = dstEchoClassCode; 417 | mDstEchoInstanceCode = dstEchoInstanceCode; 418 | mDstEchoAddress = dstEchoAddress; 419 | } 420 | 421 | void EchoObject::Sender::send(EchoFrame& frame) { 422 | unsigned short tid = EchoSocket::nextTID(); 423 | frame.setTID(tid); 424 | EchoSocket::sendUDPFrame(frame); 425 | } 426 | 427 | void EchoObject::Sender::sendTCP(EchoFrame& frame) { 428 | unsigned short tid = EchoSocket::nextTID(); 429 | frame.setTID(tid); 430 | //EchoSocket::sendTCPFrame(frame); 431 | } 432 | 433 | EchoObject::Setter::Setter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 434 | , std::string dstEchoAddress, bool responseRequired) 435 | : EchoObject::Sender(NodeProfile::ECHO_CLASS_CODE 436 | , Echo::getSelfNode()->getNodeProfile()->getInstanceCode() 437 | , dstEchoClassCode, dstEchoInstanceCode, dstEchoAddress 438 | , responseRequired ? EchoFrame::ESV_SETC : EchoFrame::ESV_SETI) { 439 | } 440 | 441 | EchoObject::Setter::~Setter() { 442 | } 443 | 444 | EchoFrame EchoObject::Setter::send() { 445 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 446 | , mDstEchoClassCode, mDstEchoInstanceCode 447 | , mDstEchoAddress, mESV); 448 | 449 | for(int pidx = 0; pidx < mPropertyList.size(); pidx++) { 450 | EchoProperty p = mPropertyList.at(pidx); 451 | frame.addProperty(p); 452 | } 453 | Sender::send(frame); 454 | return frame; 455 | } 456 | 457 | EchoFrame EchoObject::Setter::sendTCP() { 458 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 459 | , mDstEchoClassCode, mDstEchoInstanceCode 460 | , mDstEchoAddress, mESV); 461 | 462 | for(int pidx = 0; pidx < mPropertyList.size(); pidx++) { 463 | EchoProperty p = mPropertyList.at(pidx); 464 | frame.addProperty(p); 465 | } 466 | Sender::sendTCP(frame); 467 | return frame; 468 | } 469 | 470 | EchoObject::Setter& EchoObject::Setter::reqSetProperty(unsigned char epc, 471 | std::vector edt) { 472 | mPropertyList.push_back(EchoProperty(epc, edt)); 473 | return (*this); 474 | } 475 | 476 | EchoObject::Getter::Getter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 477 | , std::string dstEchoAddress) 478 | : EchoObject::Sender(NodeProfile::ECHO_CLASS_CODE 479 | , Echo::getSelfNode()->getNodeProfile()->getInstanceCode() 480 | , dstEchoClassCode, dstEchoInstanceCode, dstEchoAddress 481 | , EchoFrame::ESV_GET) { 482 | } 483 | 484 | EchoObject::Getter::~Getter() { 485 | } 486 | 487 | EchoFrame EchoObject::Getter::send() { 488 | 489 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 490 | , mDstEchoClassCode, mDstEchoInstanceCode 491 | , mDstEchoAddress, mESV); 492 | 493 | 494 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 495 | unsigned char epc = mEPCList.at(eidx); 496 | frame.addProperty(EchoProperty(epc)); 497 | } 498 | Sender::send(frame); 499 | return frame; 500 | } 501 | 502 | EchoFrame EchoObject::Getter::sendTCP() { 503 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 504 | , mDstEchoClassCode, mDstEchoInstanceCode 505 | , mDstEchoAddress, mESV); 506 | 507 | 508 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 509 | unsigned char epc = mEPCList.at(eidx); 510 | frame.addProperty(EchoProperty(epc)); 511 | } 512 | Sender::sendTCP(frame); 513 | return frame; 514 | } 515 | 516 | EchoObject::Getter& EchoObject::Getter::reqGetProperty(unsigned char epc) { 517 | mEPCList.push_back(epc); 518 | return (*this); 519 | } 520 | 521 | EchoObject::Informer::Informer(unsigned short echoClassCode, unsigned char echoInstanceCode 522 | , std::string dstEchoAddress, bool isSelfObject) 523 | : EchoObject::Sender 524 | ( isSelfObject ? echoClassCode : NodeProfile::ECHO_CLASS_CODE 525 | , isSelfObject ? echoInstanceCode : Echo::getSelfNode()->getNodeProfile()->getInstanceCode() 526 | , isSelfObject ? NodeProfile::ECHO_CLASS_CODE : echoClassCode 527 | , isSelfObject ? NodeProfile::INSTANCE_CODE : echoInstanceCode 528 | , dstEchoAddress 529 | , isSelfObject ? EchoFrame::ESV_INF : EchoFrame::ESV_INF_REQ) { 530 | } 531 | 532 | EchoObject::Informer::~Informer() { 533 | } 534 | 535 | EchoFrame EchoObject::Informer::send() { 536 | 537 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 538 | , mDstEchoClassCode, mDstEchoInstanceCode 539 | , mDstEchoAddress, mESV); 540 | 541 | if(mESV == EchoFrame::ESV_INF_REQ) { 542 | 543 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 544 | unsigned char epc = mEPCList.at(eidx); 545 | frame.addProperty(EchoProperty(epc)); 546 | } 547 | } else { 548 | std::shared_ptr node = Echo::getSelfNode(); 549 | if(node.get() == NULL) { 550 | return frame; 551 | } 552 | std::shared_ptr seoj = node.get()->getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode); 553 | if(seoj.get() == NULL) { 554 | return frame; 555 | } 556 | 557 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 558 | unsigned char epc = mEPCList.at(eidx); 559 | std::shared_ptr > edt = seoj.get()->getProperty(epc); 560 | 561 | if(edt.get() != nullptr) { 562 | EchoProperty property(epc, *(edt.get())); 563 | 564 | if(seoj.get()->isValidProperty(property)) { 565 | frame.addProperty(property); 566 | } 567 | } 568 | } 569 | } 570 | 571 | Sender::send(frame); 572 | return frame; 573 | } 574 | 575 | EchoFrame EchoObject::Informer::sendTCP() { 576 | 577 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 578 | , mDstEchoClassCode, mDstEchoInstanceCode 579 | , mDstEchoAddress, mESV); 580 | 581 | if(mESV == EchoFrame::ESV_INF_REQ) { 582 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 583 | unsigned char epc = mEPCList.at(eidx); 584 | frame.addProperty(EchoProperty(epc)); 585 | } 586 | } else { 587 | std::shared_ptr node = Echo::getSelfNode(); 588 | if(node.get() == NULL) { 589 | return frame; 590 | } 591 | std::shared_ptr seoj = node.get()->getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode); 592 | if(seoj.get() == NULL) { 593 | return frame; 594 | } 595 | 596 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 597 | unsigned char epc = mEPCList.at(eidx); 598 | std::shared_ptr > edt = seoj.get()->getProperty(epc); 599 | if(edt.get() != nullptr) { 600 | EchoProperty property(epc, *(edt.get())); 601 | 602 | if(seoj.get()->isValidProperty(property)) { 603 | frame.addProperty(property); 604 | } 605 | } 606 | } 607 | } 608 | 609 | Sender::sendTCP(frame); 610 | return frame; 611 | } 612 | 613 | EchoObject::Informer& EchoObject::Informer::reqInformProperty(unsigned char epc) { 614 | mEPCList.push_back(epc); 615 | return (*this); 616 | } 617 | 618 | EchoObject::InformerC::InformerC(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 619 | , std::string dstEchoAddress) 620 | : EchoObject::Sender(NodeProfile::ECHO_CLASS_CODE 621 | , Echo::getSelfNode()->getNodeProfile()->getInstanceCode() 622 | , NodeProfile::ECHO_CLASS_CODE, NodeProfile::INSTANCE_CODE, dstEchoAddress 623 | , EchoFrame::ESV_INFC) { 624 | } 625 | 626 | EchoObject::InformerC::~InformerC() { 627 | } 628 | 629 | EchoFrame EchoObject::InformerC::send() { 630 | 631 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 632 | , mDstEchoClassCode, mDstEchoInstanceCode 633 | , mDstEchoAddress, mESV); 634 | 635 | std::shared_ptr node = Echo::getSelfNode(); 636 | if(node.get() == NULL) { 637 | return frame; 638 | } 639 | std::shared_ptr seoj = node.get()->getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode); 640 | if(seoj.get() == NULL) { 641 | return frame; 642 | } 643 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 644 | unsigned char epc = mEPCList.at(eidx); 645 | std::shared_ptr > edt = seoj.get()->getProperty(epc); 646 | 647 | if(edt.get() != nullptr) { 648 | EchoProperty property(epc, *(edt.get())); 649 | 650 | if(seoj.get()->isValidProperty(property)) { 651 | frame.addProperty(property); 652 | } 653 | } 654 | } 655 | 656 | Sender::send(frame); 657 | return frame; 658 | } 659 | 660 | EchoFrame EchoObject::InformerC::sendTCP() { 661 | 662 | EchoFrame frame(mSrcEchoClassCode, mSrcEchoInstanceCode 663 | , mDstEchoClassCode, mDstEchoInstanceCode 664 | , mDstEchoAddress, mESV); 665 | 666 | std::shared_ptr node = Echo::getSelfNode(); 667 | if(node.get() == NULL) { 668 | return frame; 669 | } 670 | std::shared_ptr seoj = node.get()->getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode); 671 | if(seoj.get() == NULL) { 672 | return frame; 673 | } 674 | for(int eidx = 0; eidx < mEPCList.size(); eidx++) { 675 | unsigned char epc = mEPCList.at(eidx); 676 | std::shared_ptr > edt = seoj.get()->getProperty(epc); 677 | 678 | if(edt.get() != nullptr) { 679 | EchoProperty property(epc, *(edt.get())); 680 | 681 | if(seoj.get()->isValidProperty(property)) { 682 | frame.addProperty(property); 683 | } 684 | } 685 | } 686 | 687 | Sender::sendTCP(frame); 688 | return frame; 689 | } 690 | 691 | EchoObject::InformerC& EchoObject::InformerC::reqInformProperty(unsigned char epc) { 692 | mEPCList.push_back(epc); 693 | return (*this); 694 | } 695 | 696 | std::shared_ptr EchoObject::getReceiver() { 697 | return mReceiver; 698 | } 699 | 700 | }; 701 | 702 | -------------------------------------------------------------------------------- /src/openecho/EchoObject.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoObject.h 3 | * 4 | * Created on: 2013/10/23 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOOBJECT_H_ 9 | #define ECHOOBJECT_H_ 10 | 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace sonycsl_openecho { 18 | 19 | class EchoFrame; 20 | class EchoNode; 21 | class EchoProperty; 22 | 23 | class EchoObject { 24 | public: 25 | class Receiver; 26 | class Setter; 27 | class Getter; 28 | class Informer; 29 | class InformerC; 30 | 31 | private: 32 | std::shared_ptr mReceiver; 33 | std::shared_ptr mNode; 34 | protected: 35 | std::shared_ptr > mAnnouncementProperties; 36 | std::shared_ptr > mSetProperties; 37 | std::shared_ptr > mGetProperties; 38 | public: 39 | EchoObject(); 40 | virtual ~EchoObject(); 41 | 42 | private: 43 | void operator =(const EchoObject& src); // non-copyable 44 | EchoObject(const EchoObject& src); // non-copyable 45 | 46 | public: 47 | void init(); 48 | virtual void onNew(std::shared_ptr eoj); 49 | virtual void onFound(std::shared_ptr eoj); 50 | unsigned char getClassGroupCode(); 51 | unsigned char getClassCode(); 52 | virtual unsigned char getInstanceCode() = 0; 53 | virtual unsigned short getEchoClassCode() = 0; 54 | long getEchoObjectCode(); 55 | 56 | void setNode(std::shared_ptr node); 57 | std::shared_ptr getNode(); 58 | void removeNode(); 59 | 60 | bool isProxy(); 61 | bool isSelfObject(); 62 | 63 | virtual bool setProperty(EchoProperty& property); 64 | virtual std::shared_ptr > getProperty(unsigned char epc); 65 | virtual bool isValidProperty(EchoProperty& property); 66 | 67 | void addStatusChangeAnnouncementProperty(unsigned char epc); 68 | void removeStatusChangeAnnouncementProperty(unsigned char epc); 69 | void clearStatusChangeAnnouncementProperties(); 70 | std::shared_ptr > getStatusChangeAnnouncementProperties(); 71 | 72 | void addSetProperty(unsigned char epc); 73 | void removeSetProperty(unsigned char epc); 74 | void clearSetProperties(); 75 | std::shared_ptr > getSetProperties(); 76 | 77 | void addGetProperty(unsigned char epc); 78 | void removeGetProperty(unsigned char epc); 79 | void clearGetProperties(); 80 | std::shared_ptr > getGetProperties(); 81 | 82 | void setReceiver(std::shared_ptr receiver); 83 | std::shared_ptr getReceiver(); 84 | 85 | EchoFrame onReceiveRequest(EchoFrame& frame); 86 | 87 | EchoObject::Setter set(); 88 | EchoObject::Setter set(bool responseRequired); 89 | EchoObject::Getter get(); 90 | EchoObject::Informer inform(); 91 | 92 | protected: 93 | EchoObject::Informer inform(bool multicast); 94 | EchoObject::InformerC informC(std::string address); 95 | 96 | virtual void setupPropertyMaps(); 97 | void onReceiveSetRequest(EchoProperty& property, EchoFrame& response); 98 | void onReceiveGetRequest(unsigned char epc, EchoFrame& response); 99 | 100 | 101 | public: 102 | class Receiver { 103 | public: 104 | Receiver(); 105 | virtual ~Receiver(); 106 | public: 107 | void onReceive(std::shared_ptr eoj, EchoFrame& frame); 108 | protected: 109 | virtual void onReceiveFrame(std::shared_ptr eoj, EchoFrame& frame); 110 | virtual bool onSetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 111 | virtual bool onGetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 112 | virtual bool onInformProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property); 113 | }; 114 | 115 | class Sender { 116 | protected: 117 | unsigned short mSrcEchoClassCode; 118 | unsigned char mSrcEchoInstanceCode; 119 | unsigned short mDstEchoClassCode; 120 | unsigned char mDstEchoInstanceCode; 121 | std::string mDstEchoAddress; 122 | unsigned char mESV; 123 | public: 124 | Sender(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 125 | , unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 126 | , std::string dstEchoAddress, unsigned char esv); 127 | virtual ~Sender(); 128 | void setSeoj(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode); 129 | void setDeoj(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 130 | , std::string dstEchoAddress); 131 | protected: 132 | virtual EchoFrame send() = 0; 133 | void send(EchoFrame& frame); 134 | virtual EchoFrame sendTCP() = 0; 135 | void sendTCP(EchoFrame& frame); 136 | 137 | }; 138 | 139 | class Setter : public EchoObject::Sender { 140 | private: 141 | std::vector mPropertyList; 142 | public: 143 | Setter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 144 | , std::string dstEchoAddress, bool responseRequired); 145 | virtual ~Setter(); 146 | EchoFrame send(); 147 | EchoFrame sendTCP(); 148 | virtual EchoObject::Setter& reqSetProperty(unsigned char epc, std::vector edt); 149 | }; 150 | 151 | class Getter : public EchoObject::Sender { 152 | private: 153 | std::vector mEPCList; 154 | public: 155 | Getter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 156 | , std::string dstEchoAddress); 157 | virtual ~Getter(); 158 | EchoFrame send(); 159 | EchoFrame sendTCP(); 160 | virtual EchoObject::Getter& reqGetProperty(unsigned char epc); 161 | }; 162 | 163 | class Informer : public EchoObject::Sender { 164 | private: 165 | std::vector mEPCList; 166 | public: 167 | Informer(unsigned short echoClassCode, unsigned char echoInstanceCode 168 | , std::string dstEchoAddress, bool isSelfObject); 169 | virtual ~Informer(); 170 | EchoFrame send(); 171 | EchoFrame sendTCP(); 172 | virtual EchoObject::Informer& reqInformProperty(unsigned char epc); 173 | }; 174 | 175 | class InformerC : public EchoObject::Sender { 176 | private: 177 | std::vector mEPCList; 178 | public: 179 | InformerC(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 180 | , std::string dstEchoAddress); 181 | virtual ~InformerC(); 182 | EchoFrame send(); 183 | EchoFrame sendTCP(); 184 | virtual EchoObject::InformerC& reqInformProperty(unsigned char epc); 185 | }; 186 | }; 187 | 188 | 189 | }; 190 | 191 | #endif /* ECHOOBJECT_H_ */ 192 | -------------------------------------------------------------------------------- /src/openecho/EchoProperty.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoProperty.cpp 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoProperty.h" 9 | 10 | 11 | namespace sonycsl_openecho { 12 | 13 | EchoProperty::EchoProperty(const unsigned char _epc) 14 | : epc(_epc) 15 | , pdc(0) { 16 | } 17 | EchoProperty::EchoProperty(const unsigned char _epc, 18 | const std::vector _edt) 19 | : epc(_epc) 20 | , pdc((unsigned char)(_edt.size())) 21 | , edt(_edt) { 22 | } 23 | 24 | EchoProperty::~EchoProperty() { 25 | // TODO Auto-generated destructor stub 26 | } 27 | 28 | int EchoProperty::size() const { 29 | return ((int)pdc + 2); 30 | } 31 | 32 | EchoProperty::EchoProperty(const EchoProperty& property) 33 | : epc(property.epc) 34 | , pdc(property.pdc) 35 | , edt(property.edt) { 36 | } 37 | 38 | EchoProperty& EchoProperty::operator =(const EchoProperty& property) { 39 | epc = property.epc; 40 | pdc = property.pdc; 41 | edt = property.edt; 42 | 43 | return (*this); 44 | } 45 | 46 | }; 47 | -------------------------------------------------------------------------------- /src/openecho/EchoProperty.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoProperty.h 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOPROPERTY_H_ 9 | #define ECHOPROPERTY_H_ 10 | 11 | #include "OpenECHO.h" 12 | 13 | 14 | namespace sonycsl_openecho { 15 | 16 | class EchoProperty { 17 | public: 18 | unsigned char epc; 19 | unsigned char pdc; 20 | std::vector edt; 21 | public: 22 | EchoProperty(const unsigned char _epc); 23 | EchoProperty(const unsigned char _epc, const std::vector _edt); 24 | virtual ~EchoProperty(); 25 | EchoProperty(const EchoProperty& property); 26 | EchoProperty &operator =(const EchoProperty &property); 27 | int size() const; 28 | }; 29 | 30 | }; 31 | 32 | #endif /* ECHOPROPERTY_H_ */ 33 | -------------------------------------------------------------------------------- /src/openecho/EchoProtocol.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoProtocol.cpp 3 | * 4 | * Created on: 2014/03/17 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoProtocol.h" 9 | #include "OpenECHO.h" 10 | 11 | 12 | namespace sonycsl_openecho { 13 | 14 | EchoProtocol::EchoProtocol() { 15 | // TODO Auto-generated constructor stub 16 | 17 | } 18 | 19 | EchoProtocol::~EchoProtocol() { 20 | // TODO Auto-generated destructor stub 21 | } 22 | 23 | EchoTask::EchoTask(EchoFrame frame) : mFrame(new EchoFrame(frame)){ 24 | } 25 | 26 | EchoTask::~EchoTask() { 27 | delete mFrame; 28 | } 29 | 30 | void EchoTask::perform() { 31 | checkObjectInFrame(); 32 | if(isReportFrame()) { 33 | onReceiveReport(); 34 | } 35 | if(isRequestFrame()) { 36 | std::vector responses = onReceiveRequest(); 37 | for(EchoFrame res : responses) { 38 | 39 | if(res.getESV() == EchoFrame::ESV_SET_NO_RES) { 40 | return; 41 | } 42 | if(res.getESV() == EchoFrame::ESV_INF) { 43 | res.setDstEchoAddress(EchoSocket::MULTICAST_ADDRESS); 44 | informAll(res); 45 | } else { 46 | respond(res); 47 | } 48 | } 49 | } 50 | } 51 | 52 | bool EchoTask::isRequestFrame() { 53 | switch(mFrame->getESV()) { 54 | case EchoFrame::ESV_SETI: case EchoFrame::ESV_SETC: 55 | case EchoFrame::ESV_GET: 56 | case EchoFrame::ESV_INF_REQ: 57 | case EchoFrame::ESV_SET_GET: 58 | case EchoFrame::ESV_INFC: 59 | return true; 60 | default: 61 | return false; 62 | } 63 | } 64 | 65 | bool EchoTask::isReportFrame() { 66 | switch(mFrame->getESV()) { 67 | case EchoFrame::ESV_SETI_SNA: 68 | case EchoFrame::ESV_SET_RES: case EchoFrame::ESV_SETC_SNA: 69 | case EchoFrame::ESV_GET_RES: case EchoFrame::ESV_GET_SNA: 70 | case EchoFrame::ESV_INF: case EchoFrame::ESV_INF_SNA: 71 | case EchoFrame::ESV_INFC_RES: 72 | case EchoFrame::ESV_INFC: 73 | return true; 74 | default: 75 | return false; 76 | } 77 | } 78 | 79 | std::vector EchoTask::onReceiveRequest() { 80 | 81 | std::vector responses; 82 | std::shared_ptr selfNode = Echo::getSelfNode(); 83 | if(selfNode.get() == nullptr) { 84 | return responses; 85 | } 86 | if(mFrame->getDstEchoInstanceCode() == 0) { 87 | if(mFrame->getDstEchoClassCode() == NodeProfile::ECHO_CLASS_CODE) { 88 | std::shared_ptr deoj = selfNode.get()->getNodeProfile(); 89 | EchoFrame res = onReceiveRequest(deoj); 90 | responses.push_back(res); 91 | } else { 92 | //DeviceObject[] deojList = selfNode.getDevices(frame.getDstEchoClassCode()); 93 | std::vector > deojList = selfNode.get()->getDevices(mFrame->getDstEchoClassCode()); 94 | for(std::shared_ptr deoj : deojList) { 95 | EchoFrame res = onReceiveRequest(deoj); 96 | responses.push_back(res); 97 | } 98 | } 99 | } else { 100 | std::shared_ptr deoj = selfNode.get()->getInstance(mFrame->getDstEchoClassCode(), mFrame->getDstEchoInstanceCode()); 101 | if(deoj.get() == nullptr) {return responses;} 102 | EchoFrame res = onReceiveRequest(deoj); 103 | responses.push_back(res); 104 | } 105 | return responses; 106 | } 107 | 108 | EchoFrame EchoTask::onReceiveRequest( 109 | std::shared_ptr deoj) { 110 | EchoFrame request = *mFrame; // copy 111 | request.setDstEchoInstanceCode(deoj.get()->getInstanceCode()); 112 | EchoFrame response = deoj.get()->onReceiveRequest(request); 113 | 114 | return response; 115 | } 116 | 117 | void EchoTask::onReceiveReport() { 118 | std::shared_ptr node = Echo::getNode(mFrame->getSrcEchoAddress()); 119 | std::shared_ptr seoj = node.get()->getInstance( 120 | mFrame->getSrcEchoClassCode(), mFrame->getSrcEchoInstanceCode()); 121 | 122 | if(seoj.get() == nullptr) { 123 | return; 124 | } 125 | 126 | std::shared_ptr receiver = seoj.get()->getReceiver(); 127 | if(receiver.get() != nullptr) { 128 | receiver.get()->onReceive(seoj, *mFrame); 129 | } 130 | } 131 | 132 | void EchoTask::checkObjectInFrame() { 133 | if(EchoSocket::SELF_ADDRESS == mFrame->getSrcEchoAddress()) { 134 | // self node 135 | return; 136 | } 137 | 138 | // other node 139 | std::shared_ptr node = Echo::getNode(mFrame->getSrcEchoAddress()); 140 | bool flagNewNode = false; 141 | if(node.get() == nullptr) { 142 | node = Echo::addOtherNode(mFrame->getSrcEchoAddress()); 143 | flagNewNode = true; 144 | if(node.get() == nullptr) {return;} 145 | 146 | node.get()->getNodeProfile().get()->setNode(node); 147 | } 148 | 149 | if(mFrame->getSrcEchoClassCode() == NodeProfile::ECHO_CLASS_CODE 150 | && mFrame->getSrcEchoInstanceCode() == NodeProfile::INSTANCE_CODE_TRANSMISSION_ONLY) { 151 | std::shared_ptr profile = node.get()->getNodeProfile(); 152 | NodeProfile::Proxy* proxy = dynamic_cast(profile.get()); 153 | proxy->setInstanceCode(NodeProfile::INSTANCE_CODE_TRANSMISSION_ONLY); 154 | } 155 | 156 | bool flagNewDevice = false; 157 | std::shared_ptr seoj = node.get()->getInstance(mFrame->getSrcEchoClassCode(), mFrame->getSrcEchoInstanceCode()); 158 | if(seoj.get() == nullptr) { 159 | // generate 160 | // device 161 | 162 | seoj = node.get()->addOtherDevice(mFrame->getSrcEchoClassCode(), mFrame->getSrcEchoInstanceCode()); 163 | flagNewDevice = true; 164 | 165 | if(seoj.get() == nullptr) { 166 | if(flagNewNode) { 167 | node.get()->onNew(node); 168 | } 169 | node.get()->onFound(node); 170 | return; 171 | } else {seoj.get()->setNode(node);} 172 | } 173 | if(seoj.get()->getEchoClassCode() == NodeProfile::ECHO_CLASS_CODE 174 | && (seoj.get()->getInstanceCode() == NodeProfile::INSTANCE_CODE 175 | || seoj.get()->getInstanceCode() == NodeProfile::INSTANCE_CODE_TRANSMISSION_ONLY) 176 | && (mFrame->getESV() == EchoFrame::ESV_GET_RES 177 | || mFrame->getESV() == EchoFrame::ESV_GET_SNA 178 | || mFrame->getESV() == EchoFrame::ESV_INF 179 | || mFrame->getESV() == EchoFrame::ESV_INF_SNA 180 | || mFrame->getESV() == EchoFrame::ESV_INFC)) { 181 | // seoj is NodeProfile 182 | std::vector > foundDevices; 183 | std::vector flagNewDevices; 184 | char TRUE = 1; 185 | char FALSE = 0; 186 | 187 | for(EchoProperty p : mFrame->getPropertyList()) { 188 | if(p.epc != NodeProfile::EPC_INSTANCE_LIST_NOTIFICATION 189 | && p.epc != NodeProfile::EPC_SELF_NODE_INSTANCE_LIST_S) {continue;} 190 | if(p.pdc == 0) {continue;} 191 | int deviceListSize = (int)p.edt[0]; 192 | if(deviceListSize > 84) { 193 | deviceListSize = 84; 194 | } 195 | for(int d = 0, i = 1; d < deviceListSize; d++) { 196 | if(i == p.pdc) break; 197 | unsigned short echoClassCode = (unsigned short)(((p.edt[i]) & 0xFF) << 8); 198 | i += 1; 199 | if(i == p.pdc) break; 200 | echoClassCode += p.edt[i] & 0xFF; 201 | i += 1; 202 | if(i == p.pdc) break; 203 | unsigned char echoInstanceCode = p.edt[i]; 204 | i += 1; 205 | if(node.get()->containsDevice(echoClassCode, echoInstanceCode)) { 206 | flagNewDevices.push_back(FALSE); 207 | foundDevices.push_back(node.get()->getInstance(echoClassCode, echoInstanceCode)); 208 | } else { 209 | // new 210 | flagNewDevices.push_back(TRUE); 211 | std::shared_ptr eoj = node.get()->addOtherDevice(echoClassCode, echoInstanceCode); 212 | foundDevices.push_back(eoj); 213 | if(eoj.get() != nullptr) {eoj->setNode(node);} 214 | } 215 | } 216 | } 217 | 218 | if(flagNewNode) { 219 | node.get()->onNew(node); 220 | } 221 | node.get()->onFound(node); 222 | if(flagNewDevice) { 223 | seoj.get()->onNew(seoj); 224 | } 225 | seoj.get()->onFound(seoj); 226 | int foundDeviceListSize = foundDevices.size(); 227 | for(int i = 0; i < foundDeviceListSize; i++) { 228 | if(flagNewDevices.at(i) == TRUE) { 229 | //Echo.getEventListener().onNewEchoObject(foundDevices.get(i)); 230 | foundDevices.at(i).get()->onNew(foundDevices.at(i)); 231 | } 232 | //Echo.getEventListener().onFoundEchoObject(foundDevices.get(i)); 233 | foundDevices.at(i).get()->onFound(foundDevices.at(i)); 234 | } 235 | } else { 236 | // seoj is DeviceObject 237 | if(flagNewNode) { 238 | //Echo.getEventListener().onNewNode(node); 239 | node.get()->onNew(node); 240 | } 241 | //Echo.getEventListener().onFoundNode(node); 242 | node.get()->onFound(node); 243 | if(flagNewDevice) { 244 | //Echo.getEventListener().onNewEchoObject(seoj); 245 | seoj.get()->onNew(seoj); 246 | } 247 | //Echo.getEventListener().onFoundEchoObject(seoj); 248 | seoj.get()->onFound(seoj); 249 | return; 250 | } 251 | } 252 | 253 | }; 254 | -------------------------------------------------------------------------------- /src/openecho/EchoProtocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoProtocol.h 3 | * 4 | * Created on: 2014/03/17 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOPROTOCOL_H_ 9 | #define ECHOPROTOCOL_H_ 10 | 11 | #include 12 | #include 13 | 14 | namespace sonycsl_openecho { 15 | 16 | class EchoFrame; 17 | class EchoObject; 18 | 19 | class EchoProtocol { 20 | public: 21 | EchoProtocol(); 22 | virtual ~EchoProtocol(); 23 | 24 | virtual void receive() = 0; 25 | }; 26 | 27 | class EchoTask { 28 | protected: 29 | EchoFrame* mFrame; 30 | public: 31 | EchoTask(EchoFrame frame); 32 | virtual ~EchoTask(); 33 | 34 | void perform(); 35 | protected: 36 | virtual void respond(EchoFrame& response) = 0; 37 | virtual void informAll(EchoFrame& response) = 0; 38 | 39 | bool isRequestFrame(); 40 | bool isReportFrame(); 41 | 42 | std::vector onReceiveRequest(); 43 | EchoFrame onReceiveRequest(std::shared_ptr deoj); 44 | void onReceiveReport(); 45 | void checkObjectInFrame(); 46 | 47 | }; 48 | 49 | 50 | }; 51 | 52 | #endif /* ECHOPROTOCOL_H_ */ 53 | -------------------------------------------------------------------------------- /src/openecho/EchoSocket.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoSocket.cpp 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | #include "OpenECHO.h" 8 | 9 | #include "EchoSocket.h" 10 | 11 | namespace sonycsl_openecho { 12 | 13 | 14 | const std::string EchoSocket::SELF_ADDRESS = "127.0.0.1"; 15 | const std::string EchoSocket::MULTICAST_ADDRESS = "224.0.23.0"; 16 | bool EchoSocket::sRunningReceiverThread = false; 17 | bool EchoSocket::sRunningPerformerThread = false; 18 | EchoUDPProtocol EchoSocket::sUDPProtocol; 19 | unsigned short EchoSocket::sNextTID = 0; 20 | std::queue > EchoSocket::sTaskQueue; 21 | 22 | 23 | EchoSocket::EchoSocket() { 24 | // TODO Auto-generated constructor stub 25 | 26 | } 27 | 28 | EchoSocket::~EchoSocket() { 29 | // TODO Auto-generated destructor stub 30 | } 31 | 32 | 33 | void EchoSocket::openSocket() { 34 | //startReceiverThread(); 35 | sUDPProtocol.openUDP(); 36 | } 37 | 38 | void EchoSocket::closeSocket() { 39 | //stopReceiverThread(); 40 | //close(sReceiverSock); 41 | //shutdown(sReceiverSock); 42 | sUDPProtocol.closeUDP(); 43 | } 44 | void EchoSocket::enqueueTask(std::shared_ptr task) { 45 | //receiveUDPFrame(frame); 46 | sTaskQueue.push(task); 47 | } 48 | 49 | std::shared_ptr EchoSocket::dequeueTask() { 50 | if(!sTaskQueue.empty()) { 51 | std::shared_ptr task = sTaskQueue.front(); 52 | sTaskQueue.pop(); 53 | return task; 54 | } else { 55 | return std::shared_ptr(); 56 | } 57 | } 58 | 59 | void EchoSocket::sendUDPFrame(EchoFrame& frame) { 60 | sUDPProtocol.sendUDP(frame); 61 | } 62 | 63 | 64 | void EchoSocket::startReceiverThread() { 65 | pthread_t thread; 66 | pthread_create(&thread, NULL, receiverThread, NULL); 67 | pthread_t thread1; 68 | pthread_create(&thread1, NULL, taskPerformerThread, NULL); 69 | } 70 | 71 | void EchoSocket::stopReceiverThread() { 72 | sRunningReceiverThread = false; 73 | sRunningPerformerThread = false; 74 | } 75 | void EchoSocket::resumeReceiverThread() { 76 | } 77 | 78 | void EchoSocket::pauseReceiverThread() { 79 | } 80 | 81 | unsigned short EchoSocket::nextTID() { 82 | unsigned short ret = sNextTID; 83 | sNextTID += 1; 84 | //Echo::getStorage().get()->setNextTID(sNextTID); 85 | return ret; 86 | } 87 | 88 | short EchoSocket::getNextTIDNoIncrement() { 89 | return sNextTID; 90 | } 91 | 92 | void* EchoSocket::receiverThread(void*) { 93 | 94 | sRunningReceiverThread = true; 95 | while(sRunningReceiverThread) { 96 | receive(); 97 | usleep(10000); 98 | } 99 | return 0; 100 | } 101 | 102 | void* EchoSocket::taskPerformerThread(void*) { 103 | sRunningPerformerThread = true; 104 | while(sRunningPerformerThread) { 105 | performTasks(); 106 | usleep(10000); 107 | } 108 | return 0; 109 | } 110 | 111 | void EchoSocket::receive() { 112 | sUDPProtocol.receive(); 113 | } 114 | 115 | void EchoSocket::performTasks() { 116 | std::shared_ptr task = dequeueTask(); 117 | while(task.get() != nullptr) { 118 | task.get()->perform(); 119 | task = dequeueTask(); 120 | } 121 | } 122 | 123 | }; 124 | -------------------------------------------------------------------------------- /src/openecho/EchoSocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoSocket.h 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOSOCKET_H_ 9 | #define ECHOSOCKET_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | //#include 15 | 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | 30 | namespace sonycsl_openecho { 31 | 32 | class EchoFrame; 33 | class EchoNode; 34 | class EchoObject; 35 | class EchoTask; 36 | class EchoUDPProtocol; 37 | 38 | 39 | class EchoSocket { 40 | public: 41 | static const std::string SELF_ADDRESS; 42 | static const std::string MULTICAST_ADDRESS; 43 | static std::queue > sTaskQueue; 44 | protected: 45 | static EchoUDPProtocol sUDPProtocol; 46 | static bool sRunningReceiverThread; 47 | static bool sRunningPerformerThread; 48 | static unsigned short sNextTID; 49 | 50 | private: 51 | EchoSocket(); 52 | ~EchoSocket(); 53 | EchoSocket(const EchoSocket& rhs); 54 | EchoSocket& operator=(const EchoSocket& rhs); 55 | 56 | 57 | //static void sendFrameToSelfNode(EchoFrame& frame); 58 | //static void receiveFrameFromSelfNode(); 59 | //static void onReceiveUDPFrame(EchoFrame& frame); 60 | //static void onReceiveUDPRequsetFrame(const std::shared_ptr deoj, EchoFrame& frame); 61 | //static void onReceiveNotRequest(EchoFrame& frame); 62 | //static void checkNewObjectInResponse(EchoFrame& frame, const std::shared_ptr node, const std::shared_ptr seoj 63 | // , const bool flagNewNode, const bool flagNewDevice); 64 | 65 | public: 66 | static void enqueueTask(std::shared_ptr task); 67 | static std::shared_ptr dequeueTask(); 68 | static void openSocket(); 69 | static void closeSocket(); 70 | static void sendUDPFrame(EchoFrame& frame); 71 | //static void sendTCPFrame(EchoFrame& frame); 72 | 73 | //static void receiveUDP(); 74 | //static void receiveTCP(); 75 | static void startReceiverThread(); 76 | static void stopReceiverThread(); 77 | static void resumeReceiverThread(); 78 | static void pauseReceiverThread(); 79 | 80 | static unsigned short nextTID(); 81 | static short getNextTIDNoIncrement(); 82 | 83 | static void* receiverThread(void*); 84 | static void* taskPerformerThread(void*); 85 | static void receive(); 86 | static void performTasks(); 87 | }; 88 | 89 | }; 90 | 91 | #endif /* ECHOSOCKET_H_ */ 92 | -------------------------------------------------------------------------------- /src/openecho/EchoStorage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoStorage.cpp 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoStorage.h" 9 | 10 | namespace sonycsl_openecho { 11 | 12 | EchoStorage::EchoStorage() { 13 | mTID = 0; 14 | 15 | } 16 | 17 | EchoStorage::~EchoStorage() { 18 | // TODO Auto-generated destructor stub 19 | } 20 | 21 | unsigned short EchoStorage::getInitialTID() { 22 | return mTID; 23 | } 24 | 25 | void EchoStorage::setNextTID(unsigned short tid) { 26 | mTID = tid; 27 | } 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /src/openecho/EchoStorage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoStorage.h 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOSTORAGE_H_ 9 | #define ECHOSTORAGE_H_ 10 | 11 | #include 12 | 13 | 14 | namespace sonycsl_openecho { 15 | 16 | 17 | class EchoStorage { 18 | protected: 19 | unsigned short mTID; 20 | public: 21 | EchoStorage(); 22 | virtual ~EchoStorage(); 23 | 24 | // TID 25 | unsigned short getInitialTID(); 26 | void setNextTID(unsigned short tid); 27 | 28 | }; 29 | 30 | }; 31 | 32 | #endif /* ECHOSTORAGE_H_ */ 33 | -------------------------------------------------------------------------------- /src/openecho/EchoUDPProtocol.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoUDPProtocol.cpp 3 | * 4 | * Created on: 2014/03/17 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoUDPProtocol.h" 9 | #include "OpenECHO.h" 10 | 11 | namespace sonycsl_openecho { 12 | 13 | const int EchoUDPProtocol::UDP_MAX_PACKET_SIZE = 65507; 14 | const int EchoUDPProtocol::PORT = 3610; 15 | 16 | EchoUDPProtocol::EchoUDPProtocol() { 17 | // TODO Auto-generated constructor stub 18 | mReceiverSock = 0; 19 | 20 | } 21 | 22 | EchoUDPProtocol::~EchoUDPProtocol() { 23 | // TODO Auto-generated destructor stub 24 | } 25 | 26 | void EchoUDPProtocol::openUDP() { 27 | mReceiverSock = socket(AF_INET, SOCK_DGRAM, 0); 28 | 29 | mReceiverSockAddr.sin_family = AF_INET; 30 | mReceiverSockAddr.sin_port = htons(PORT); 31 | mReceiverSockAddr.sin_addr.s_addr = INADDR_ANY; 32 | 33 | bind(mReceiverSock, (struct sockaddr *) &mReceiverSockAddr, 34 | sizeof(mReceiverSockAddr)); 35 | 36 | int val = 1; 37 | ioctl(mReceiverSock, FIONBIO, &val); 38 | 39 | memset(&mReceiverIpMreq, 0, sizeof(mReceiverIpMreq)); 40 | mReceiverIpMreq.imr_interface.s_addr = INADDR_ANY; 41 | mReceiverIpMreq.imr_multiaddr.s_addr = inet_addr( 42 | EchoSocket::MULTICAST_ADDRESS.c_str()); 43 | 44 | u_char loop = 0; // 0 = invalid, 1 = valid(default); 45 | if (setsockopt(mReceiverSock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 46 | sizeof(loop)) != 0) { 47 | perror("EchoUDPProtocol::open()[IP_MULTICAST_LOOP]"); 48 | return; 49 | } 50 | 51 | in_addr_t ipaddr = inet_addr(EchoSocket::SELF_ADDRESS.c_str()); 52 | if (setsockopt(mReceiverSock, IPPROTO_IP, IP_MULTICAST_IF, (char *) &ipaddr, 53 | sizeof(ipaddr)) != 0) { 54 | perror("EchoUDPProtocol::open()[IP_MULTICAST_IF]"); 55 | return; 56 | } 57 | 58 | if (setsockopt(mReceiverSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 59 | (char *) &mReceiverIpMreq, sizeof(mReceiverIpMreq)) != 0) { 60 | perror("EchoUDPProtocol::open()[IP_ADD_MEMBERSHIP]"); 61 | return; 62 | } 63 | 64 | } 65 | 66 | void EchoUDPProtocol::closeUDP() { 67 | 68 | close(mReceiverSock); 69 | } 70 | 71 | void EchoUDPProtocol::sendUDP(EchoFrame& frame) { 72 | if (frame.getDstEchoAddress() == EchoSocket::SELF_ADDRESS) { 73 | Echo::getEventListenerDelegate().onSendFrame(frame); 74 | 75 | sendToSelf(frame); 76 | return; 77 | } 78 | int sock; 79 | struct sockaddr_in addr; 80 | 81 | sock = socket(AF_INET, SOCK_DGRAM, 0); 82 | 83 | addr.sin_family = AF_INET; 84 | addr.sin_port = htons(PORT); 85 | addr.sin_addr.s_addr = inet_addr(frame.getDstEchoAddress().c_str()); 86 | 87 | /* 88 | if(frame.getDstEchoAddress() == EchoSocket::MULTICAST_ADDRESS) { 89 | in_addr_t ipaddr = inet_addr(EchoSocket::SELF_ADDRESS.c_str()); 90 | 91 | if (setsockopt(sock, 92 | IPPROTO_IP, 93 | IP_MULTICAST_IF, 94 | (char *)&ipaddr, sizeof(ipaddr)) != 0) { 95 | perror("sendUDPFrame"); 96 | return; 97 | } 98 | } 99 | */ 100 | 101 | 102 | std::vector byteArray = frame.getFrameByteArray(); 103 | int size = byteArray.size(); 104 | unsigned char buffer[size]; 105 | for (int i = 0; i < size; i++) { 106 | buffer[i] = byteArray.at(i); 107 | } 108 | 109 | sendto(sock, buffer, byteArray.size(), 0, (struct sockaddr *) &addr, 110 | sizeof(addr)); 111 | 112 | //std::cerr << "sendUDPFrame:" << std::hex; 113 | //for (int i = 0; i < size; i++) { 114 | // std::cerr << (int) (buffer[i]) << " "; 115 | //} 116 | //std::cerr << ":" << frame.getDstEchoAddress() << std::endl; 117 | 118 | close(sock); 119 | Echo::getEventListenerDelegate().onSendFrame(frame); 120 | 121 | if (frame.getDstEchoAddress() == EchoSocket::MULTICAST_ADDRESS) { 122 | EchoFrame f = frame; 123 | f.setDstEchoAddress(EchoSocket::SELF_ADDRESS); 124 | sendToSelf(f); 125 | } 126 | } 127 | 128 | void EchoUDPProtocol::sendToSelf(EchoFrame& frame) { 129 | 130 | Echo::getEventListenerDelegate().onReceiveFrame(frame); 131 | std::shared_ptr task( 132 | (EchoTask*) (new UDPProtocolTask(frame, *this))); 133 | EchoSocket::enqueueTask(task); 134 | } 135 | 136 | void EchoUDPProtocol::receive() { 137 | 138 | int size; 139 | unsigned char buffer[UDP_MAX_PACKET_SIZE]; 140 | 141 | struct sockaddr_in from; 142 | unsigned int addrSize = sizeof(struct sockaddr_in); 143 | memset(buffer, 0, sizeof(buffer)); 144 | size = recvfrom(mReceiverSock, buffer, sizeof(buffer), 0, 145 | (struct sockaddr *) &from, &addrSize); 146 | if (size < EchoFrame::MIN_FRAME_SIZE) { 147 | return; 148 | } 149 | std::vector data; 150 | for (int i = 0; i < size; i++) { 151 | data.push_back(buffer[i]); 152 | } 153 | 154 | const char* caddress = inet_ntoa(from.sin_addr); 155 | std::string address = caddress; 156 | 157 | //std::cerr << "receiveUDPFrame:" << std::hex; 158 | //for (int i = 0; i < size; i++) { 159 | // std::cerr << (int) (buffer[i]) << " "; 160 | //} 161 | //std::cerr << ":" << address << std::endl; 162 | 163 | // check address 164 | /* 165 | int fd; 166 | struct ifreq ifr; 167 | fd = socket(AF_INET, SOCK_DGRAM, 0); 168 | ifr.ifr_addr.sa_family = AF_INET; 169 | strncpy(ifr.ifr_name, "en1", IFNAMSIZ-1); 170 | ioctl(fd, SIOCGIFADDR, &ifr); 171 | close(fd); 172 | const char* selfCaddress = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); 173 | std::string selfAddress = selfCaddress; 174 | if(address == selfAddress) { 175 | return; 176 | }*/ 177 | if (isLoopback(address)) { 178 | return; 179 | } 180 | 181 | EchoFrame frame(address, data); 182 | Echo::getEventListenerDelegate().onReceiveFrame(frame); 183 | 184 | std::shared_ptr task( 185 | (EchoTask*) (new UDPProtocolTask(frame, *this))); 186 | EchoSocket::enqueueTask(task); 187 | 188 | } 189 | 190 | bool EchoUDPProtocol::isLoopback(std::string address) { 191 | struct ifaddrs * ifAddrStruct = NULL; 192 | struct ifaddrs * ifa = NULL; 193 | void * tmpAddrPtr = NULL; 194 | 195 | getifaddrs(&ifAddrStruct); 196 | 197 | for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { 198 | if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4 199 | // is a valid IP4 Address 200 | tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 201 | char addressBuffer[INET_ADDRSTRLEN]; 202 | inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); 203 | //printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); 204 | std::string selfAddress = addressBuffer; 205 | if (address == selfAddress) { 206 | 207 | if (ifAddrStruct != NULL) 208 | freeifaddrs(ifAddrStruct); 209 | return true; 210 | } 211 | } else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6 212 | // is a valid IP6 Address 213 | tmpAddrPtr = &((struct sockaddr_in6 *) ifa->ifa_addr)->sin6_addr; 214 | char addressBuffer[INET6_ADDRSTRLEN]; 215 | inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); 216 | //printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); 217 | std::string selfAddress = addressBuffer; 218 | 219 | if (address == selfAddress) { 220 | 221 | if (ifAddrStruct != NULL) 222 | freeifaddrs(ifAddrStruct); 223 | return true; 224 | } 225 | } 226 | } 227 | if (ifAddrStruct != NULL) 228 | freeifaddrs(ifAddrStruct); 229 | return false; 230 | } 231 | 232 | UDPProtocolTask::UDPProtocolTask(EchoFrame frame, EchoUDPProtocol& protocol) : 233 | EchoTask(frame), mProtocol(protocol) { 234 | } 235 | 236 | UDPProtocolTask::~UDPProtocolTask() { 237 | } 238 | 239 | void UDPProtocolTask::respond(EchoFrame& response) { 240 | mProtocol.sendUDP(response); 241 | } 242 | 243 | void UDPProtocolTask::informAll(EchoFrame& response) { 244 | mProtocol.sendUDP(response); 245 | } 246 | 247 | } 248 | ; 249 | -------------------------------------------------------------------------------- /src/openecho/EchoUDPProtocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoUDPProtocol.h 3 | * 4 | * Created on: 2014/03/17 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOUDPPROTOCOL_H_ 9 | #define ECHOUDPPROTOCOL_H_ 10 | 11 | #include "EchoProtocol.h" 12 | 13 | #include 14 | #include 15 | #include 16 | //#include 17 | 18 | #include // for memset. 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | 36 | 37 | namespace sonycsl_openecho { 38 | 39 | class EchoUDPProtocol: public EchoProtocol { 40 | public: 41 | static const int UDP_MAX_PACKET_SIZE; 42 | static const int PORT; 43 | protected: 44 | 45 | int mReceiverSock; 46 | struct sockaddr_in mReceiverSockAddr; 47 | struct ip_mreq mReceiverIpMreq; 48 | //bool mRunningReceiverThread; 49 | 50 | public: 51 | EchoUDPProtocol(); 52 | virtual ~EchoUDPProtocol(); 53 | 54 | void openUDP(); 55 | void closeUDP(); 56 | 57 | void sendUDP(EchoFrame& frame); 58 | void sendToSelf(EchoFrame& frame); 59 | 60 | virtual void receive(); 61 | 62 | protected: 63 | bool isLoopback(std::string address); 64 | 65 | }; 66 | 67 | class UDPProtocolTask : EchoTask { 68 | protected: 69 | EchoUDPProtocol& mProtocol; 70 | public: 71 | UDPProtocolTask(EchoFrame frame, EchoUDPProtocol& protocol); 72 | virtual ~UDPProtocolTask(); 73 | 74 | protected: 75 | virtual void respond(EchoFrame& response); 76 | virtual void informAll(EchoFrame& response); 77 | }; 78 | 79 | }; 80 | 81 | #endif /* ECHOUDPPROTOCOL_H_ */ 82 | -------------------------------------------------------------------------------- /src/openecho/EchoUtils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoUtils.cpp 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "EchoUtils.h" 9 | 10 | namespace sonycsl_openecho { 11 | 12 | std::map EchoUtils::sAllocatedSelfDeviceInstanceCode; 13 | 14 | long EchoUtils::convertToEchoObjectCode(unsigned short echoClassCode, 15 | unsigned char instanceCode) { 16 | long ret = (long)echoClassCode; 17 | ret = ret << 8; 18 | ret += (long)instanceCode; 19 | return ret; 20 | } 21 | 22 | std::shared_ptr > EchoUtils::convertPropertiesToPropertyMap( 23 | std::shared_ptr > properties) { 24 | std::shared_ptr > ret(new std::vector()); 25 | int propertiesSize = properties.get()->size(); 26 | if(propertiesSize < 16) { 27 | ret.get()->resize(propertiesSize+1); 28 | (*(ret.get()))[0] = propertiesSize; 29 | for(int i = 0; i < propertiesSize; i++) { 30 | std::set::iterator itr = properties.get()->begin(); 31 | for(int j = 0; j < i; j++) {++itr;} 32 | (*(ret.get()))[i] = *itr; 33 | } 34 | } else { 35 | ret.get()->resize(17); 36 | (*(ret.get()))[0] = propertiesSize; 37 | for(int i = 0; i < ret.get()->size(); i++) { 38 | (*(ret.get()))[i] = 0; 39 | } 40 | for(int i = 0; i < propertiesSize; i++) { 41 | std::set::iterator itr = properties.get()->begin(); 42 | for(int j = 0; j < i; j++) {++itr;} 43 | unsigned char p = *itr; 44 | int high = (int)((p >> 4) & 0x0F); 45 | if (high < 0x08) { 46 | continue; 47 | } 48 | int low = (int)(p & 0x0F); 49 | (*(ret.get()))[low+1] = (((*(ret.get()))[low+1] & 0xFF) | (1 << (high - 0x08))); 50 | } 51 | 52 | } 53 | return ret; 54 | } 55 | 56 | std::shared_ptr > EchoUtils::convertPropertyMapToProperties( 57 | std::shared_ptr > propertyMap) { 58 | std::shared_ptr > ret(new std::set()); 59 | if(propertyMap.get() == nullptr || propertyMap.get()->size() == 0) {return ret;} 60 | int propertiesSize = propertyMap.get()->at(0); 61 | if(propertiesSize < 16) { 62 | for(int i = 0; i < propertiesSize; i++) { 63 | ret.get()->insert(propertyMap.get()->at(i+1)); 64 | } 65 | } else { 66 | for(int low = 0; low < 16; low++) { 67 | unsigned char tmp = propertyMap.get()->at(low + 1); 68 | for(int high = 0; high < 8; high++) { 69 | if((tmp & 0x01) == 0x01) { 70 | ret.get()->insert((unsigned char)(low | ((high + 0x08) << 4))); 71 | } 72 | tmp = tmp >> 1; 73 | } 74 | } 75 | } 76 | return ret; 77 | 78 | } 79 | 80 | unsigned char EchoUtils::allocateSelfDeviceInstanceCode( 81 | unsigned short echoClassCode) { 82 | 83 | if (sAllocatedSelfDeviceInstanceCode.find(echoClassCode) == sAllocatedSelfDeviceInstanceCode.end()) { 84 | sAllocatedSelfDeviceInstanceCode.insert(std::map::value_type(echoClassCode, 1)); 85 | return 1; 86 | } else { 87 | unsigned char code = sAllocatedSelfDeviceInstanceCode[echoClassCode]; 88 | code += 1; 89 | sAllocatedSelfDeviceInstanceCode[echoClassCode] += 1; 90 | return sAllocatedSelfDeviceInstanceCode[echoClassCode]; 91 | } 92 | } 93 | 94 | }; 95 | -------------------------------------------------------------------------------- /src/openecho/EchoUtils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EchoUtils.h 3 | * 4 | * Created on: 2013/10/22 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef ECHOUTILS_H_ 9 | #define ECHOUTILS_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace sonycsl_openecho { 17 | 18 | class EchoUtils { 19 | private: 20 | EchoUtils(); 21 | ~EchoUtils(); 22 | EchoUtils(const EchoUtils& rhs); 23 | EchoUtils& operator=(const EchoUtils& rhs); 24 | public: 25 | static long convertToEchoObjectCode(unsigned short echoClassCode, unsigned char instanceCode); 26 | static std::shared_ptr > convertPropertiesToPropertyMap(std::shared_ptr > properties); 27 | static std::shared_ptr > convertPropertyMapToProperties(std::shared_ptr > propertyMap); 28 | 29 | private: 30 | static std::map sAllocatedSelfDeviceInstanceCode; 31 | public: 32 | static unsigned char allocateSelfDeviceInstanceCode(unsigned short echoClassCode); 33 | }; 34 | 35 | }; 36 | 37 | #endif /* ECHOUTILS_H_ */ 38 | -------------------------------------------------------------------------------- /src/openecho/HouseholdSolarPowerGeneration.h: -------------------------------------------------------------------------------- 1 | #ifndef HouseholdSolarPowerGeneration_H_ 2 | #define HouseholdSolarPowerGeneration_H_ 3 | 4 | #include "EchoObject.h" 5 | #include "DeviceObject.h" 6 | namespace sonycsl_openecho 7 | { 8 | 9 | class HouseholdSolarPowerGeneration : public DeviceObject 10 | { 11 | public: 12 | class Receiver; 13 | class Setter; 14 | class Getter; 15 | class Informer; 16 | class InformerC; 17 | class Proxy; 18 | 19 | public: 20 | static const short ECHO_CLASS_CODE; 21 | static const unsigned char EPC_OPERATION_STATUS; 22 | static const unsigned char EPC_SYSTEM_INTERCONNECTION_INFORMATION; 23 | static const unsigned char EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION; 24 | static const unsigned char EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION; 25 | static const unsigned char EPC_RESETTING_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION; 26 | static const unsigned char EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_SOLD; 27 | static const unsigned char EPC_RESETTING_CUMULATIVE_AMOUT_OF_ELECTRICITY_SOLD; 28 | static const unsigned char EPC_POWER_GENERATION_OUTPUT_LIMIT_SETTING_1; 29 | static const unsigned char EPC_POWER_GENERATION_OUTPUT_LIMIT_SETTING_2; 30 | static const unsigned char EPC_LIMIT_SETTING_FOR_AMOUNT_OF_ELECTRICITY_SOLD; 31 | static const unsigned char EPC_RATED_POWER_GENERATION_OUTPUT_INTERCONNECTED; 32 | static const unsigned char EPC_RATED_POWER_GENERATION_OUTPUT_INDEPENDENT; 33 | 34 | protected: 35 | unsigned char mEchoInstanceCode; 36 | std::shared_ptr> mStandardVersionInformation; 37 | 38 | public: 39 | HouseholdSolarPowerGeneration(); 40 | virtual ~HouseholdSolarPowerGeneration(); 41 | 42 | public: 43 | virtual unsigned char getInstanceCode(); 44 | virtual void onNew(std::shared_ptr eoj); 45 | virtual void allocateSelfDeviceInstanceCode(); 46 | 47 | virtual bool setProperty(EchoProperty &property); 48 | virtual std::shared_ptr> getProperty(unsigned char epc); 49 | virtual bool isValidProperty(EchoProperty &property); 50 | HouseholdSolarPowerGeneration::Setter set(); 51 | HouseholdSolarPowerGeneration::Setter set(bool responseRequired); 52 | HouseholdSolarPowerGeneration::Getter get(); 53 | HouseholdSolarPowerGeneration::Informer inform(); 54 | 55 | protected: 56 | HouseholdSolarPowerGeneration::Informer inform(bool multicast); 57 | HouseholdSolarPowerGeneration::InformerC informC(std::string address); 58 | virtual void setupPropertyMaps(); 59 | virtual bool setOperationStatus(std::vector &edt); 60 | virtual std::shared_ptr> getOperationStatus() = 0; 61 | virtual bool isValidSystemInterconnEctedType(std::vector &edt); 62 | virtual std::shared_ptr> getMeasuredCumulativeAmountOfElectricityGenerated()=0; 63 | virtual bool isValidMeasuredCumulativeAmountOfElectricityGenerated(std::vector &edt); 64 | virtual std::shared_ptr> getMeasuredInstantaneousAmountOfElectricityGenerated()=0; 65 | virtual bool isValidMeasuredInstantaneousAmountOfElectricityGenerated(std::vector &edt); 66 | virtual bool isValidResettingCumulativeAmountOfElectricityGenerated(std::vector &edt); 67 | virtual bool isValidResettingCumulativeAmountOfElectricitySold(std::vector &edt); 68 | virtual std::shared_ptr> getMeasuredCumulativeAmountOfElectricitySold(); 69 | virtual bool isValidMeasuredCumulativeAmountOfElectricitySold(std::vector &edt); 70 | virtual std::shared_ptr> getPowerGenerationOutputLimitSetting1(); 71 | virtual std::shared_ptr> getPowerGenerationOutputLimitSetting2(); 72 | virtual std::shared_ptr> getSystemInterconnectedType(); 73 | virtual std::shared_ptr> getLimitSettingForTheAmountOfElectricitySold(); 74 | virtual std::shared_ptr> getRatedPowerGenerationOutputSystemInterconnected(); 75 | virtual bool isValidPowerGenerationOutputLimitSetting1(std::vector &edt); 76 | virtual bool isValidPowerGenerationOutputLimitSetting2(std::vector &edt); 77 | virtual bool isValidRatedPowerGenerationOutputSystemInterconnected(std::vector &edt); 78 | virtual bool isValidLimitSettingForTheAmountOfElectricitySold(std::vector &edt); 79 | virtual std::shared_ptr> getRatedPowerGenerationOutputIndependent(); 80 | virtual bool isValidRatedPowerGenerationOutputIndependent(std::vector &edt); 81 | virtual bool setResettingCumulativeAmountOfElectricityGenerated(std::vector &edt); 82 | virtual bool setResettingCumulativeAmountOfElectricitySold(std::vector &edt); 83 | virtual bool setPowerGenerationOutputLimitSetting1(std::vector &edt); 84 | virtual bool setPowerGenerationOutputLimitSetting2(std::vector &edt); 85 | virtual bool setLimitSettingForTheAmountOfElectricitySold(std::vector &edt); 86 | virtual bool setRatedPowerGenerationOutputIndependent(std::vector &edt); 87 | virtual bool setRatedPowerGenerationOutputSystemInterconnected(std::vector &edt); 88 | 89 | public: 90 | class Receiver : public EchoObject::Receiver 91 | { 92 | public: 93 | Receiver(); 94 | virtual ~Receiver(); 95 | 96 | protected: 97 | virtual bool onSetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 98 | virtual bool onGetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 99 | virtual void onGetOperationStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 100 | virtual void onSetOperationStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 101 | virtual void onSetResettingCumulativeAmountOfElectricitySold(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 102 | virtual void onSetResettingCumulativeAmountOfElectricityGenerated(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 103 | virtual void onSetPowerGenerationOutputLimitSetting1(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 104 | virtual void onSetPowerGenerationOutputLimitSetting2(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 105 | virtual void onSetLimitSettingForTheAmountOfElectricitySold(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 106 | virtual void onSetRatedPowerGenerationOutputSystemInterconnected(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 107 | virtual void onSetRatedPowerGenerationOutputIndependent(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 108 | virtual void onGetSystemInterconnEctedType(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 109 | virtual void onGetMeasuredInstantaneousAmountOfElectricityGenerated(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 110 | virtual void onGetMeasuredCumulativeAmountOfElectricityGenerated(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 111 | virtual void onGetMeasuredCumulativeAmountOfElectricitySold(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 112 | virtual void onGetPowerGenerationOutputLimitSetting1(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 113 | virtual void onGetPowerGenerationOutputLimitSetting2(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 114 | virtual void onGetLimitSettingForTheAmountOfElectricitySold(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 115 | virtual void onGetRatedPowerGenerationOutputSystemInterconnected(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 116 | virtual void onGetRatedPowerGenerationOutputIndependent(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty &property, bool success); 117 | 118 | }; 119 | 120 | class Setter : public EchoObject::Setter 121 | { 122 | public: 123 | Setter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode, std::string dstEchoAddress, bool responseRequired); 124 | virtual ~Setter(); 125 | virtual HouseholdSolarPowerGeneration::Setter& reqSetLimitSettingForTheAmountOfElectricitySold(std::vector &edt); 126 | virtual HouseholdSolarPowerGeneration::Setter& reqSetResettingCumulativeAmountOfElectricityGenerated(std::vector &edt); 127 | virtual HouseholdSolarPowerGeneration::Setter& reqSetResettingCumulativeAmountOfElectricitySold(std::vector &edt); 128 | virtual HouseholdSolarPowerGeneration::Setter& reqSetPowerGenerationOutputLimitSetting1(std::vector &edt); 129 | virtual HouseholdSolarPowerGeneration::Setter& reqSetPowerGenerationOutputLimitSetting2(std::vector &edt); 130 | virtual HouseholdSolarPowerGeneration::Setter& reqSetProperty(unsigned char epc, std::vector edt); 131 | virtual HouseholdSolarPowerGeneration::Setter& reqSetRatedPowerGenerationOutputSystemInterconnected(std::vector &edt); 132 | virtual HouseholdSolarPowerGeneration::Setter& reqSetRatedPowerGenerationOutputIndependent(std::vector &edt); 133 | virtual HouseholdSolarPowerGeneration::Setter& reqSetOperationStatus(std::vector edt); 134 | virtual HouseholdSolarPowerGeneration::Setter& reqSetInstallationLocation(std::vector edt); 135 | virtual HouseholdSolarPowerGeneration::Setter& reqSetCurrentLimitSetting(std::vector edt); 136 | virtual HouseholdSolarPowerGeneration::Setter& reqSetPowerSavingOperationSetting(std::vector edt); 137 | virtual HouseholdSolarPowerGeneration::Setter& reqSetRemoteControlSetting(std::vector edt); 138 | virtual HouseholdSolarPowerGeneration::Setter& reqSetCurrentTimeSetting(std::vector edt); 139 | virtual HouseholdSolarPowerGeneration::Setter& reqSetCurrentDateSetting(std::vector edt); 140 | virtual HouseholdSolarPowerGeneration::Setter& reqSetPowerLimitSetting(std::vector edt); 141 | }; 142 | class Getter : public EchoObject::Getter 143 | { 144 | public: 145 | Getter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode, std::string dstEchoAddress); 146 | virtual ~Getter(); 147 | virtual HouseholdSolarPowerGeneration::Getter& reqGetProperty(unsigned char epc); 148 | virtual HouseholdSolarPowerGeneration::Getter& reqGetOperationStatus(); 149 | virtual HouseholdSolarPowerGeneration::Getter& reqGetSystemInterconnEctedType(); 150 | virtual HouseholdSolarPowerGeneration::Getter& reqGetMeasuredInstantaneousAmountOfElectricityGenerated(); 151 | virtual HouseholdSolarPowerGeneration::Getter& reqGetMeasuredCumulativeAmountOfElectricityGenerated(); 152 | virtual HouseholdSolarPowerGeneration::Getter& reqGetMeasuredCumulativeAmountOfElectricitySold(); 153 | virtual HouseholdSolarPowerGeneration::Getter& reqGetPowerGenerationOutputLimitSetting1(); 154 | virtual HouseholdSolarPowerGeneration::Getter& reqGetPowerGenerationOutputLimitSetting2(); 155 | virtual HouseholdSolarPowerGeneration::Getter& reqGetLimitSettingForTheAmountOfElectricitySold(); 156 | virtual HouseholdSolarPowerGeneration::Getter& reqGetRatedPowerGenerationOutputSystemInterconnected(); 157 | virtual HouseholdSolarPowerGeneration::Getter& reqGetRatedPowerGenerationOutputIndependent(); 158 | }; 159 | class Informer : public EchoObject::Informer 160 | { 161 | public: 162 | Informer(unsigned short echoClassCode, unsigned char echoInstanceCode, std::string dstEchoAddress, bool isSelfObject); 163 | virtual ~Informer(); 164 | virtual HouseholdSolarPowerGeneration::Informer& reqInformProperty(unsigned char epc); 165 | virtual HouseholdSolarPowerGeneration::Informer& reqInformOperationStatus(); 166 | virtual HouseholdSolarPowerGeneration::Informer& reqInformSystemInterconnEctedType(); 167 | virtual HouseholdSolarPowerGeneration::Informer& reqInformMeasuredInstantaneousAmountOfElectricityGenerated(); 168 | virtual HouseholdSolarPowerGeneration::Informer& reqInformMeasuredCumulativeAmountOfElectricityGenerated(); 169 | virtual HouseholdSolarPowerGeneration::Informer& reqInformMeasuredCumulativeAmountOfElectricitySold(); 170 | virtual HouseholdSolarPowerGeneration::Informer& reqInformPowerGenerationOutputLimitSetting1(); 171 | virtual HouseholdSolarPowerGeneration::Informer& reqInformPowerGenerationOutputLimitSetting2(); 172 | virtual HouseholdSolarPowerGeneration::Informer& reqInformLimitSettingForTheAmountOfElectricitySold(); 173 | virtual HouseholdSolarPowerGeneration::Informer& reqInformRatedPowerGenerationOutputSystemInterconnected(); 174 | virtual HouseholdSolarPowerGeneration::Informer& reqInformRatedPowerGenerationOutputIndependent(); 175 | 176 | }; 177 | class InformerC : public EchoObject::InformerC 178 | { 179 | public: 180 | InformerC(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode, std::string dstEchoAddress); 181 | virtual ~InformerC(); 182 | // virtual HouseholdSolarPowerGeneration::InformerC &reqInformProperty(unsigned char epc); 183 | }; 184 | 185 | 186 | }; 187 | class HouseholdSolarPowerGeneration::Proxy : public HouseholdSolarPowerGeneration 188 | { 189 | protected: 190 | unsigned short mEchoClassCode; 191 | virtual std::shared_ptr> getOperationStatus(); 192 | virtual bool setInstallationLocation(std::vector &edt); 193 | virtual std::shared_ptr> getInstallationLocation(); 194 | virtual std::shared_ptr> getFaultStatus(); 195 | virtual std::shared_ptr> getManufacturerCode(); 196 | 197 | public: 198 | Proxy(unsigned short echoClassCode, unsigned char echoInstanceCode); 199 | virtual ~Proxy(); 200 | virtual unsigned char getInstanceCode(); 201 | virtual unsigned short getEchoClassCode(); 202 | }; 203 | 204 | }; 205 | #endif /* HouseholdSolarPowerGeneration_H_ */ -------------------------------------------------------------------------------- /src/openecho/NodeProfile.h: -------------------------------------------------------------------------------- 1 | /* 2 | * NodeProfile.h 3 | * 4 | * Created on: 2013/10/29 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef NODEPROFILE_H_ 9 | #define NODEPROFILE_H_ 10 | 11 | #include "ProfileObject.h" 12 | 13 | namespace sonycsl_openecho { 14 | 15 | class NodeProfile: public ProfileObject { 16 | public: 17 | class Receiver; 18 | class Setter; 19 | class Getter; 20 | class Informer; 21 | class InformerC; 22 | class Proxy; 23 | 24 | public: 25 | static const unsigned short ECHO_CLASS_CODE; 26 | static const unsigned char INSTANCE_CODE; 27 | static const unsigned char INSTANCE_CODE_TRANSMISSION_ONLY; 28 | 29 | static const unsigned char EPC_OPERATING_STATUS; 30 | static const unsigned char EPC_VERSION_INFORMATION; 31 | static const unsigned char EPC_IDENTIFICATION_NUMBER; 32 | static const unsigned char EPC_FAULT_CONTENT; 33 | static const unsigned char EPC_UNIQUE_IDENTIFIER_DATA; 34 | static const unsigned char EPC_NUMBER_OF_SELF_NODE_INSTANCES; 35 | static const unsigned char EPC_NUMBER_OF_SELF_NODE_CLASSES; 36 | static const unsigned char EPC_INSTANCE_LIST_NOTIFICATION; 37 | static const unsigned char EPC_SELF_NODE_INSTANCE_LIST_S; 38 | static const unsigned char EPC_SELF_NODE_CLASS_LIST; 39 | 40 | protected: 41 | std::shared_ptr > mVersionInformation; 42 | 43 | public: 44 | NodeProfile(); 45 | virtual ~NodeProfile(); 46 | 47 | virtual void onNew(std::shared_ptr eoj); 48 | 49 | virtual unsigned char getInstanceCode(); 50 | virtual unsigned short getEchoClassCode(); 51 | 52 | public: 53 | virtual bool setProperty(EchoProperty& property); 54 | virtual std::shared_ptr > getProperty(unsigned char epc); 55 | virtual bool isValidProperty(EchoProperty& property); 56 | 57 | NodeProfile::Setter set(); 58 | NodeProfile::Setter set(bool responseRequired); 59 | NodeProfile::Getter get(); 60 | NodeProfile::Informer inform(); 61 | 62 | protected: 63 | NodeProfile::Informer inform(bool multicast); 64 | NodeProfile::InformerC informC(std::string address); 65 | 66 | virtual void setupPropertyMaps(); 67 | 68 | virtual bool setOperatingStatus(std::vector& edt); 69 | virtual std::shared_ptr > getOperatingStatus() = 0; 70 | virtual bool isValidOperatingStatus(std::vector& edt); 71 | virtual std::shared_ptr > getVersionInformation(); 72 | virtual bool isValidVersionInformation(std::vector& edt); 73 | virtual std::shared_ptr > getIdentificationNumber() = 0; 74 | virtual bool isValidIdentificationNumber(std::vector& edt); 75 | virtual std::shared_ptr > getFaultContent(); 76 | virtual bool isValidFaultContent(std::vector& edt); 77 | virtual bool setUniqueIdentifierData(std::vector& edt); 78 | virtual std::shared_ptr > getUniqueIdentifierData(); 79 | virtual bool isValidUniqueIdentifierData(std::vector& edt); 80 | virtual std::shared_ptr > getNumberOfSelfNodeInstances(); 81 | virtual bool isValidNumberOfSelfNodeInstances(std::vector& edt); 82 | virtual std::shared_ptr > getNumberOfSelfNodeClasses(); 83 | virtual bool isValidNumberOfSelfNodeClasses(std::vector& edt); 84 | virtual std::shared_ptr > getInstanceListNotification(); 85 | virtual bool isValidInstanceListNotification(std::vector& edt); 86 | virtual std::shared_ptr > getSelfNodeInstanceListS(); 87 | virtual bool isValidSelfNodeInstanceListS(std::vector& edt); 88 | virtual std::shared_ptr > getSelfNodeClassList(); 89 | virtual bool isValidSelfNodeClassList(std::vector& edt); 90 | 91 | 92 | public: 93 | class Receiver : public ProfileObject::Receiver { 94 | public: 95 | Receiver(); 96 | virtual ~Receiver(); 97 | protected: 98 | virtual bool onSetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 99 | virtual bool onGetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 100 | virtual bool onInformProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property); 101 | 102 | virtual void onSetOperatingStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 103 | virtual void onSetUniqueIdentifierData(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 104 | 105 | virtual void onGetOperatingStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 106 | virtual void onGetVersionInformation(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 107 | virtual void onGetIdentificationNumber(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 108 | virtual void onGetFaultContent(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 109 | virtual void onGetUniqueIdentifierData(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 110 | virtual void onGetNumberOfSelfNodeInstances(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 111 | virtual void onGetNumberOfSelfNodeClasses(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 112 | virtual void onGetInstanceListNotification(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 113 | virtual void onGetSelfNodeInstanceListS(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 114 | virtual void onGetSelfNodeClassList(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 115 | 116 | virtual void onInformInstanceListNotification(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property); 117 | 118 | }; 119 | 120 | class Setter : public ProfileObject::Setter { 121 | public: 122 | Setter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 123 | , std::string dstEchoAddress, bool responseRequired); 124 | virtual ~Setter(); 125 | virtual NodeProfile::Setter& reqSetProperty(unsigned char epc, std::vector edt); 126 | 127 | virtual NodeProfile::Setter& reqSetOperatingStatus(std::vector edt); 128 | virtual NodeProfile::Setter& reqSetUniqueIdentifierData(std::vector edt); 129 | 130 | }; 131 | 132 | class Getter : public ProfileObject::Getter { 133 | public: 134 | Getter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 135 | , std::string dstEchoAddress); 136 | virtual ~Getter(); 137 | virtual NodeProfile::Getter& reqGetProperty(unsigned char epc); 138 | 139 | virtual NodeProfile::Getter& reqGetFaultStatus(); 140 | virtual NodeProfile::Getter& reqGetManufacturerCode(); 141 | virtual NodeProfile::Getter& reqGetPlaceOfBusinessCode(); 142 | virtual NodeProfile::Getter& reqGetProductCode(); 143 | virtual NodeProfile::Getter& reqGetSerialNumber(); 144 | virtual NodeProfile::Getter& reqGetDateOfManufacture(); 145 | virtual NodeProfile::Getter& reqGetStatusChangeAnnouncementPropertyMap(); 146 | virtual NodeProfile::Getter& reqGetSetPropertyMap(); 147 | virtual NodeProfile::Getter& reqGetGetPropertyMap(); 148 | 149 | virtual NodeProfile::Getter& reqGetOperatingStatus(); 150 | virtual NodeProfile::Getter& reqGetVersionInformation(); 151 | virtual NodeProfile::Getter& reqGetIdentificationNumber(); 152 | virtual NodeProfile::Getter& reqGetFaultContent(); 153 | virtual NodeProfile::Getter& reqGetUniqueIdentifierData(); 154 | virtual NodeProfile::Getter& reqGetNumberOfSelfNodeInstances(); 155 | virtual NodeProfile::Getter& reqGetNumberOfSelfNodeClasses(); 156 | virtual NodeProfile::Getter& reqGetSelfNodeInstanceListS(); 157 | virtual NodeProfile::Getter& reqGetSelfNodeClassList(); 158 | }; 159 | 160 | class Informer : public ProfileObject::Informer { 161 | public: 162 | Informer(unsigned short echoClassCode, unsigned char echoInstanceCode 163 | , std::string dstEchoAddress, bool isSelfObject); 164 | virtual ~Informer(); 165 | virtual NodeProfile::Informer& reqInformProperty(unsigned char epc); 166 | 167 | virtual NodeProfile::Informer& reqInformFaultStatus(); 168 | virtual NodeProfile::Informer& reqInformManufacturerCode(); 169 | virtual NodeProfile::Informer& reqInformPlaceOfBusinessCode(); 170 | virtual NodeProfile::Informer& reqInformProductCode(); 171 | virtual NodeProfile::Informer& reqInformSerialNumber(); 172 | virtual NodeProfile::Informer& reqInformDateOfManufacture(); 173 | virtual NodeProfile::Informer& reqInformStatusChangeAnnouncementPropertyMap(); 174 | virtual NodeProfile::Informer& reqInformSetPropertyMap(); 175 | virtual NodeProfile::Informer& reqInformGetPropertyMap(); 176 | 177 | virtual NodeProfile::Informer& reqInformOperatingStatus(); 178 | virtual NodeProfile::Informer& reqInformVersionInformation(); 179 | virtual NodeProfile::Informer& reqInformIdentificationNumber(); 180 | virtual NodeProfile::Informer& reqInformFaultContent(); 181 | virtual NodeProfile::Informer& reqInformUniqueIdentifierData(); 182 | virtual NodeProfile::Informer& reqInformNumberOfSelfNodeInstances(); 183 | virtual NodeProfile::Informer& reqInformNumberOfSelfNodeClasses(); 184 | virtual NodeProfile::Informer& reqInformInstanceListNotification(); 185 | virtual NodeProfile::Informer& reqInformSelfNodeInstanceListS(); 186 | virtual NodeProfile::Informer& reqInformSelfNodeClassList(); 187 | }; 188 | 189 | class InformerC : public ProfileObject::InformerC { 190 | public: 191 | InformerC(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 192 | , std::string dstEchoAddress); 193 | virtual ~InformerC(); 194 | virtual NodeProfile::InformerC& reqInformProperty(unsigned char epc); 195 | 196 | virtual NodeProfile::InformerC& reqInformInstanceListNotification(); 197 | 198 | }; 199 | 200 | }; 201 | 202 | class NodeProfile::Proxy : public NodeProfile { 203 | protected: 204 | unsigned char mEchoInstanceCode; 205 | public: 206 | Proxy(); 207 | virtual ~Proxy(); 208 | 209 | virtual void setInstanceCode(unsigned char instanceCode); 210 | virtual unsigned char getInstanceCode(); 211 | protected: 212 | virtual std::shared_ptr > getManufacturerCode(); 213 | virtual std::shared_ptr > getOperatingStatus(); 214 | virtual std::shared_ptr > getIdentificationNumber(); 215 | }; 216 | 217 | }; 218 | 219 | #endif /* NODEPROFILE_H_ */ 220 | -------------------------------------------------------------------------------- /src/openecho/OpenECHO.h: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenECHO.h 3 | * 4 | * Created on: 2013/10/29 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef OPENECHO_H_ 9 | #define OPENECHO_H_ 10 | 11 | #include 12 | 13 | 14 | #include "Echo.h" 15 | #include "EchoSocket.h" 16 | #include "EchoFrame.h" 17 | 18 | #include "EchoUtils.h" 19 | #include "EchoStorage.h" 20 | #include "EchoProperty.h" 21 | 22 | #include "EchoNode.h" 23 | 24 | #include "EchoObject.h" 25 | #include "ProfileObject.h" 26 | #include "NodeProfile.h" 27 | #include "DeviceObject.h" 28 | 29 | #include "EchoProtocol.h" 30 | #include "EchoUDPProtocol.h" 31 | // #include "HouseholdSolarPowerGeneration.h" 32 | #endif /* OPENECHO_H_ */ 33 | -------------------------------------------------------------------------------- /src/openecho/ProfileObject.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ProfileObject.cpp 3 | * 4 | * Created on: 2013/10/29 5 | * Author: Fumiaki 6 | */ 7 | 8 | #include "ProfileObject.h" 9 | #include "OpenECHO.h" 10 | 11 | namespace sonycsl_openecho { 12 | 13 | const unsigned char ProfileObject::EPC_FAULT_STATUS = 0x88; 14 | const unsigned char ProfileObject::EPC_MANUFACTURER_CODE = 0x8A; 15 | const unsigned char ProfileObject::EPC_PLACE_OF_BUSINESS_CODE = 0x8B; 16 | const unsigned char ProfileObject::EPC_PRODUCT_CODE = 0x8C; 17 | const unsigned char ProfileObject::EPC_SERIAL_NUMBER = 0x8D; 18 | const unsigned char ProfileObject::EPC_DATE_OF_MANUFACTURE = 0x8E; 19 | const unsigned char ProfileObject::EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP = 0x9D; 20 | const unsigned char ProfileObject::EPC_SET_PROPERTY_MAP = 0x9E; 21 | const unsigned char ProfileObject::EPC_GET_PROPERTY_MAP = 0x9F; 22 | 23 | ProfileObject::ProfileObject() : EchoObject() { 24 | // TODO Auto-generated constructor stub 25 | 26 | } 27 | 28 | ProfileObject::~ProfileObject() { 29 | // TODO Auto-generated destructor stub 30 | } 31 | 32 | bool ProfileObject::setProperty(EchoProperty& property) { 33 | bool success = EchoObject::setProperty(property); 34 | if(success) return true; 35 | 36 | switch(property.epc) { 37 | default : return false; 38 | } 39 | } 40 | 41 | std::shared_ptr > ProfileObject::getProperty( 42 | unsigned char epc) { 43 | std::shared_ptr > edt = EchoObject::getProperty(epc); 44 | if(edt.get() != nullptr) return edt; 45 | 46 | switch(epc) { 47 | case EPC_FAULT_STATUS : return getFaultStatus(); 48 | case EPC_MANUFACTURER_CODE : return getManufacturerCode(); 49 | case EPC_PLACE_OF_BUSINESS_CODE : return getPlaceOfBusinessCode(); 50 | case EPC_PRODUCT_CODE : return getProductCode(); 51 | case EPC_SERIAL_NUMBER : return getSerialNumber(); 52 | case EPC_DATE_OF_MANUFACTURE : return getDateOfManufacture(); 53 | case EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP : return getStatusChangeAnnouncementPropertyMap(); 54 | case EPC_SET_PROPERTY_MAP : return getSetPropertyMap(); 55 | case EPC_GET_PROPERTY_MAP : return getGetPropertyMap(); 56 | default : return std::shared_ptr >(); 57 | } 58 | } 59 | 60 | bool ProfileObject::isValidProperty(EchoProperty& property) { 61 | bool valid = EchoObject::isValidProperty(property); 62 | if(valid) return true; 63 | 64 | switch(property.epc) { 65 | case EPC_FAULT_STATUS : return isValidFaultStatus(property.edt); 66 | case EPC_MANUFACTURER_CODE : return isValidManufacturerCode(property.edt); 67 | case EPC_PLACE_OF_BUSINESS_CODE : return isValidPlaceOfBusinessCode(property.edt); 68 | case EPC_PRODUCT_CODE : return isValidProductCode(property.edt); 69 | case EPC_SERIAL_NUMBER : return isValidSerialNumber(property.edt); 70 | case EPC_DATE_OF_MANUFACTURE : return isValidDateOfManufacture(property.edt); 71 | case EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP : return isValidStatusChangeAnnouncementPropertyMap(property.edt); 72 | case EPC_SET_PROPERTY_MAP : return isValidSetPropertyMap(property.edt); 73 | case EPC_GET_PROPERTY_MAP : return isValidGetPropertyMap(property.edt); 74 | default : return false; 75 | } 76 | } 77 | 78 | ProfileObject::Setter ProfileObject::set() { 79 | return ProfileObject::Setter(getEchoClassCode(), getInstanceCode() 80 | , getNode().get()->getAddress(), true); 81 | } 82 | 83 | ProfileObject::Setter ProfileObject::set(bool responseRequired) { 84 | 85 | return ProfileObject::Setter(getEchoClassCode(), getInstanceCode() 86 | , getNode().get()->getAddress(), responseRequired); 87 | } 88 | 89 | ProfileObject::Getter ProfileObject::get() { 90 | 91 | return ProfileObject::Getter(getEchoClassCode(), getInstanceCode() 92 | , getNode().get()->getAddress()); 93 | } 94 | 95 | ProfileObject::Informer ProfileObject::inform() { 96 | 97 | std::string address; 98 | if(isProxy()) { 99 | address = getNode().get()->getAddress(); 100 | } else { 101 | address = EchoSocket::MULTICAST_ADDRESS; 102 | } 103 | return ProfileObject::Informer(getEchoClassCode(), getInstanceCode() 104 | , address, isSelfObject()); 105 | } 106 | 107 | ProfileObject::Informer ProfileObject::inform(bool multicast) { 108 | 109 | std::string address; 110 | if(multicast) { 111 | address = EchoSocket::MULTICAST_ADDRESS; 112 | } else { 113 | address = getNode().get()->getAddress(); 114 | } 115 | return ProfileObject::Informer(getEchoClassCode(), getInstanceCode() 116 | , address, isSelfObject()); 117 | } 118 | 119 | ProfileObject::InformerC ProfileObject::informC(std::string address) { 120 | 121 | return ProfileObject::InformerC(getEchoClassCode(), getInstanceCode() 122 | , address); 123 | } 124 | 125 | void ProfileObject::setupPropertyMaps() { 126 | EchoObject::setupPropertyMaps(); 127 | 128 | addGetProperty(EPC_MANUFACTURER_CODE); 129 | addGetProperty(EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP); 130 | addGetProperty(EPC_SET_PROPERTY_MAP); 131 | addGetProperty(EPC_GET_PROPERTY_MAP); 132 | 133 | } 134 | 135 | std::shared_ptr > ProfileObject::getFaultStatus() { 136 | return std::shared_ptr >(); 137 | } 138 | 139 | bool ProfileObject::isValidFaultStatus(std::vector& edt) { 140 | if(edt.size() != 1) return false; 141 | return true; 142 | } 143 | 144 | bool ProfileObject::isValidManufacturerCode(std::vector& edt) { 145 | if(edt.size() != 3) return false; 146 | return true; 147 | } 148 | 149 | std::shared_ptr > ProfileObject::getPlaceOfBusinessCode() { 150 | return std::shared_ptr >(); 151 | } 152 | 153 | bool ProfileObject::isValidPlaceOfBusinessCode( 154 | std::vector& edt) { 155 | if(edt.size() != 3) return false; 156 | return true; 157 | } 158 | 159 | std::shared_ptr > ProfileObject::getProductCode() { 160 | return std::shared_ptr >(); 161 | } 162 | 163 | bool ProfileObject::isValidProductCode(std::vector& edt) { 164 | if(edt.size() != 12) return false; 165 | return true; 166 | } 167 | 168 | std::shared_ptr > ProfileObject::getSerialNumber() { 169 | return std::shared_ptr >(); 170 | } 171 | 172 | bool ProfileObject::isValidSerialNumber(std::vector& edt) { 173 | if(edt.size() != 12) return false; 174 | return true; 175 | } 176 | 177 | std::shared_ptr > ProfileObject::getDateOfManufacture() { 178 | return std::shared_ptr >(); 179 | } 180 | 181 | bool ProfileObject::isValidDateOfManufacture(std::vector& edt) { 182 | if(edt.size() != 4) return false; 183 | return true; 184 | } 185 | 186 | std::shared_ptr > ProfileObject::getStatusChangeAnnouncementPropertyMap() { 187 | return EchoUtils::convertPropertiesToPropertyMap(getStatusChangeAnnouncementProperties()); 188 | } 189 | 190 | bool ProfileObject::isValidStatusChangeAnnouncementPropertyMap( 191 | std::vector& edt) { 192 | if(edt.size() > 17) return false; 193 | return true; 194 | } 195 | 196 | std::shared_ptr > ProfileObject::getSetPropertyMap() { 197 | return EchoUtils::convertPropertiesToPropertyMap(getSetProperties()); 198 | } 199 | 200 | bool ProfileObject::isValidSetPropertyMap(std::vector& edt) { 201 | if(edt.size() > 17) return false; 202 | return true; 203 | } 204 | 205 | std::shared_ptr > ProfileObject::getGetPropertyMap() { 206 | return EchoUtils::convertPropertiesToPropertyMap(getGetProperties()); 207 | } 208 | 209 | bool ProfileObject::isValidGetPropertyMap(std::vector& edt) { 210 | if(edt.size() > 17) return false; 211 | return true; 212 | } 213 | 214 | ProfileObject::Receiver::Receiver() : EchoObject::Receiver() { 215 | } 216 | 217 | ProfileObject::Receiver::~Receiver() { 218 | } 219 | 220 | bool ProfileObject::Receiver::onSetProperty(std::shared_ptr eoj, 221 | unsigned short tid, unsigned char esv, EchoProperty& property, 222 | bool success) { 223 | bool ret = EchoObject::Receiver::onSetProperty(eoj, tid, esv, property, success); 224 | if(ret) return true; 225 | return false; 226 | } 227 | 228 | bool ProfileObject::Receiver::onGetProperty(std::shared_ptr eoj, 229 | unsigned short tid, unsigned char esv, EchoProperty& property, 230 | bool success) { 231 | bool ret = EchoObject::Receiver::onGetProperty(eoj, tid, esv, property, success); 232 | if(ret) return true; 233 | switch(property.epc) { 234 | case EPC_FAULT_STATUS: 235 | onGetFaultStatus(eoj, tid, esv, property, success); 236 | return true; 237 | case EPC_MANUFACTURER_CODE: 238 | onGetManufacturerCode(eoj, tid, esv, property, success); 239 | return true; 240 | case EPC_PLACE_OF_BUSINESS_CODE: 241 | onGetPlaceOfBusinessCode(eoj, tid, esv, property, success); 242 | return true; 243 | case EPC_PRODUCT_CODE: 244 | onGetProductCode(eoj, tid, esv, property, success); 245 | return true; 246 | case EPC_SERIAL_NUMBER: 247 | onGetSerialNumber(eoj, tid, esv, property, success); 248 | return true; 249 | case EPC_DATE_OF_MANUFACTURE: 250 | onGetDateOfManufacture(eoj, tid, esv, property, success); 251 | return true; 252 | case EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP: 253 | onGetStatusChangeAnnouncementPropertyMap(eoj, tid, esv, property, success); 254 | return true; 255 | case EPC_SET_PROPERTY_MAP: 256 | onGetSetPropertyMap(eoj, tid, esv, property, success); 257 | return true; 258 | case EPC_GET_PROPERTY_MAP: 259 | onGetGetPropertyMap(eoj, tid, esv, property, success); 260 | return true; 261 | default : 262 | return false; 263 | } 264 | } 265 | 266 | bool ProfileObject::Receiver::onInformProperty(std::shared_ptr eoj, 267 | unsigned short tid, unsigned char esv, EchoProperty& property) { 268 | bool ret = EchoObject::Receiver::onInformProperty(eoj, tid, esv, property); 269 | if(ret) return true; 270 | return false; 271 | } 272 | 273 | void ProfileObject::Receiver::onGetFaultStatus(std::shared_ptr eoj, 274 | unsigned short tid, unsigned char esv, EchoProperty& property, 275 | bool success) { 276 | } 277 | 278 | void ProfileObject::Receiver::onGetManufacturerCode( 279 | std::shared_ptr eoj, unsigned short tid, unsigned char esv, 280 | EchoProperty& property, bool success) { 281 | } 282 | 283 | void ProfileObject::Receiver::onGetPlaceOfBusinessCode( 284 | std::shared_ptr eoj, unsigned short tid, unsigned char esv, 285 | EchoProperty& property, bool success) { 286 | } 287 | 288 | void ProfileObject::Receiver::onGetProductCode(std::shared_ptr eoj, 289 | unsigned short tid, unsigned char esv, EchoProperty& property, 290 | bool success) { 291 | } 292 | 293 | void ProfileObject::Receiver::onGetSerialNumber(std::shared_ptr eoj, 294 | unsigned short tid, unsigned char esv, EchoProperty& property, 295 | bool success) { 296 | } 297 | 298 | void ProfileObject::Receiver::onGetDateOfManufacture( 299 | std::shared_ptr eoj, unsigned short tid, unsigned char esv, 300 | EchoProperty& property, bool success) { 301 | } 302 | 303 | void ProfileObject::Receiver::onGetStatusChangeAnnouncementPropertyMap( 304 | std::shared_ptr eoj, unsigned short tid, unsigned char esv, 305 | EchoProperty& property, bool success) { 306 | } 307 | 308 | void ProfileObject::Receiver::onGetSetPropertyMap( 309 | std::shared_ptr eoj, unsigned short tid, unsigned char esv, 310 | EchoProperty& property, bool success) { 311 | } 312 | 313 | void ProfileObject::Receiver::onGetGetPropertyMap( 314 | std::shared_ptr eoj, unsigned short tid, unsigned char esv, 315 | EchoProperty& property, bool success) { 316 | } 317 | 318 | ProfileObject::Setter::Setter(unsigned short dstEchoClassCode, 319 | unsigned char dstEchoInstanceCode, std::string dstEchoAddress, 320 | bool responseRequired) 321 | : EchoObject::Setter(dstEchoClassCode 322 | , dstEchoInstanceCode ,dstEchoAddress, responseRequired) { 323 | } 324 | 325 | ProfileObject::Setter::~Setter() { 326 | } 327 | 328 | ProfileObject::Setter& ProfileObject::Setter::reqSetProperty(unsigned char epc, 329 | std::vector edt) { 330 | return dynamic_cast(EchoObject::Setter::reqSetProperty(epc, edt)); 331 | } 332 | 333 | ProfileObject::Getter::Getter(unsigned short dstEchoClassCode, 334 | unsigned char dstEchoInstanceCode, std::string dstEchoAddress) 335 | : EchoObject::Getter(dstEchoClassCode 336 | , dstEchoInstanceCode, dstEchoAddress) { 337 | } 338 | 339 | ProfileObject::Getter::~Getter() { 340 | } 341 | 342 | ProfileObject::Getter& ProfileObject::Getter::reqGetProperty( 343 | unsigned char epc) { 344 | return dynamic_cast(EchoObject::Getter::reqGetProperty(epc)); 345 | 346 | } 347 | 348 | ProfileObject::Informer::Informer(unsigned short echoClassCode, 349 | unsigned char echoInstanceCode, std::string dstEchoAddress, 350 | bool isSelfObject) 351 | : EchoObject::Informer(echoClassCode, echoInstanceCode 352 | , dstEchoAddress, isSelfObject) { 353 | } 354 | 355 | ProfileObject::Informer::~Informer() { 356 | } 357 | 358 | ProfileObject::Informer& ProfileObject::Informer::reqInformProperty( 359 | unsigned char epc) { 360 | return dynamic_cast(EchoObject::Informer::reqInformProperty(epc)); 361 | 362 | } 363 | 364 | ProfileObject::InformerC::InformerC(unsigned short srcEchoClassCode, 365 | unsigned char srcEchoInstanceCode, std::string dstEchoAddress) 366 | : EchoObject::InformerC(srcEchoClassCode 367 | , srcEchoInstanceCode, dstEchoAddress) { 368 | } 369 | 370 | ProfileObject::InformerC::~InformerC() { 371 | } 372 | 373 | ProfileObject::InformerC& ProfileObject::InformerC::reqInformProperty( 374 | unsigned char epc) { 375 | return dynamic_cast(EchoObject::InformerC::reqInformProperty(epc)); 376 | 377 | } 378 | 379 | ProfileObject::Getter& ProfileObject::Getter::reqGetFaultStatus() { 380 | return reqGetProperty(EPC_FAULT_STATUS); 381 | } 382 | 383 | ProfileObject::Getter& ProfileObject::Getter::reqGetManufacturerCode() { 384 | return reqGetProperty(EPC_MANUFACTURER_CODE); 385 | } 386 | 387 | ProfileObject::Getter& ProfileObject::Getter::reqGetPlaceOfBusinessCode() { 388 | return reqGetProperty(EPC_PLACE_OF_BUSINESS_CODE); 389 | } 390 | 391 | ProfileObject::Getter& ProfileObject::Getter::reqGetProductCode() { 392 | return reqGetProperty(EPC_PRODUCT_CODE); 393 | } 394 | 395 | ProfileObject::Getter& ProfileObject::Getter::reqGetSerialNumber() { 396 | return reqGetProperty(EPC_SERIAL_NUMBER); 397 | } 398 | 399 | ProfileObject::Getter& ProfileObject::Getter::reqGetDateOfManufacture() { 400 | return reqGetProperty(EPC_DATE_OF_MANUFACTURE); 401 | } 402 | 403 | ProfileObject::Getter& ProfileObject::Getter::reqGetStatusChangeAnnouncementPropertyMap() { 404 | return reqGetProperty(EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP); 405 | } 406 | 407 | ProfileObject::Getter& ProfileObject::Getter::reqGetSetPropertyMap() { 408 | return reqGetProperty(EPC_SET_PROPERTY_MAP); 409 | } 410 | 411 | ProfileObject::Getter& ProfileObject::Getter::reqGetGetPropertyMap() { 412 | return reqGetProperty(EPC_GET_PROPERTY_MAP); 413 | } 414 | 415 | ProfileObject::Informer& ProfileObject::Informer::reqInformFaultStatus() { 416 | return reqInformProperty(EPC_FAULT_STATUS); 417 | } 418 | 419 | ProfileObject::Informer& ProfileObject::Informer::reqInformManufacturerCode() { 420 | return reqInformProperty(EPC_MANUFACTURER_CODE); 421 | } 422 | 423 | ProfileObject::Informer& ProfileObject::Informer::reqInformPlaceOfBusinessCode() { 424 | return reqInformProperty(EPC_PLACE_OF_BUSINESS_CODE); 425 | } 426 | 427 | ProfileObject::Informer& ProfileObject::Informer::reqInformProductCode() { 428 | return reqInformProperty(EPC_PRODUCT_CODE); 429 | } 430 | 431 | ProfileObject::Informer& ProfileObject::Informer::reqInformSerialNumber() { 432 | return reqInformProperty(EPC_SERIAL_NUMBER); 433 | } 434 | 435 | ProfileObject::Informer& ProfileObject::Informer::reqInformDateOfManufacture() { 436 | return reqInformProperty(EPC_DATE_OF_MANUFACTURE); 437 | } 438 | 439 | ProfileObject::Informer& ProfileObject::Informer::reqInformStatusChangeAnnouncementPropertyMap() { 440 | return reqInformProperty(EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP); 441 | } 442 | 443 | ProfileObject::Informer& ProfileObject::Informer::reqInformSetPropertyMap() { 444 | return reqInformProperty(EPC_SET_PROPERTY_MAP); 445 | } 446 | 447 | ProfileObject::Informer& ProfileObject::Informer::reqInformGetPropertyMap() { 448 | return reqInformProperty(EPC_GET_PROPERTY_MAP); 449 | } 450 | 451 | }; 452 | -------------------------------------------------------------------------------- /src/openecho/ProfileObject.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ProfileObject.h 3 | * 4 | * Created on: 2013/10/29 5 | * Author: Fumiaki 6 | */ 7 | 8 | #ifndef PROFILEOBJECT_H_ 9 | #define PROFILEOBJECT_H_ 10 | 11 | #include "EchoObject.h" 12 | 13 | namespace sonycsl_openecho { 14 | 15 | class ProfileObject: public EchoObject { 16 | public: 17 | class Receiver; 18 | class Setter; 19 | class Getter; 20 | class Informer; 21 | class InformerC; 22 | 23 | public: 24 | static const unsigned char EPC_FAULT_STATUS; 25 | static const unsigned char EPC_MANUFACTURER_CODE; 26 | static const unsigned char EPC_PLACE_OF_BUSINESS_CODE; 27 | static const unsigned char EPC_PRODUCT_CODE; 28 | static const unsigned char EPC_SERIAL_NUMBER; 29 | static const unsigned char EPC_DATE_OF_MANUFACTURE; 30 | static const unsigned char EPC_STATUS_CHANGE_ANNOUNCEMENT_PROPERTY_MAP; 31 | static const unsigned char EPC_SET_PROPERTY_MAP; 32 | static const unsigned char EPC_GET_PROPERTY_MAP; 33 | 34 | public: 35 | ProfileObject(); 36 | virtual ~ProfileObject(); 37 | 38 | public: 39 | virtual bool setProperty(EchoProperty& property); 40 | virtual std::shared_ptr > getProperty(unsigned char epc); 41 | virtual bool isValidProperty(EchoProperty& property); 42 | 43 | ProfileObject::Setter set(); 44 | ProfileObject::Setter set(bool responseRequired); 45 | ProfileObject::Getter get(); 46 | ProfileObject::Informer inform(); 47 | 48 | protected: 49 | ProfileObject::Informer inform(bool multicast); 50 | ProfileObject::InformerC informC(std::string address); 51 | 52 | virtual void setupPropertyMaps(); 53 | 54 | virtual std::shared_ptr > getFaultStatus(); 55 | virtual bool isValidFaultStatus(std::vector& edt); 56 | virtual std::shared_ptr > getManufacturerCode() = 0; 57 | virtual bool isValidManufacturerCode(std::vector& edt); 58 | virtual std::shared_ptr > getPlaceOfBusinessCode(); 59 | virtual bool isValidPlaceOfBusinessCode(std::vector& edt); 60 | virtual std::shared_ptr > getProductCode(); 61 | virtual bool isValidProductCode(std::vector& edt); 62 | virtual std::shared_ptr > getSerialNumber(); 63 | virtual bool isValidSerialNumber(std::vector& edt); 64 | virtual std::shared_ptr > getDateOfManufacture(); 65 | virtual bool isValidDateOfManufacture(std::vector& edt); 66 | virtual std::shared_ptr > getStatusChangeAnnouncementPropertyMap(); 67 | virtual bool isValidStatusChangeAnnouncementPropertyMap(std::vector& edt); 68 | virtual std::shared_ptr > getSetPropertyMap(); 69 | virtual bool isValidSetPropertyMap(std::vector& edt); 70 | virtual std::shared_ptr > getGetPropertyMap(); 71 | virtual bool isValidGetPropertyMap(std::vector& edt); 72 | 73 | public: 74 | class Receiver : public EchoObject::Receiver { 75 | public: 76 | Receiver(); 77 | virtual ~Receiver(); 78 | protected: 79 | virtual bool onSetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 80 | virtual bool onGetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 81 | virtual bool onInformProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property); 82 | 83 | virtual void onGetFaultStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 84 | virtual void onGetManufacturerCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 85 | virtual void onGetPlaceOfBusinessCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 86 | virtual void onGetProductCode(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 87 | virtual void onGetSerialNumber(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 88 | virtual void onGetDateOfManufacture(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 89 | virtual void onGetStatusChangeAnnouncementPropertyMap(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 90 | virtual void onGetSetPropertyMap(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 91 | virtual void onGetGetPropertyMap(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success); 92 | 93 | }; 94 | 95 | class Setter : public EchoObject::Setter { 96 | public: 97 | Setter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 98 | , std::string dstEchoAddress, bool responseRequired); 99 | virtual ~Setter(); 100 | virtual ProfileObject::Setter& reqSetProperty(unsigned char epc, std::vector edt); 101 | }; 102 | 103 | class Getter : public EchoObject::Getter { 104 | public: 105 | Getter(unsigned short dstEchoClassCode, unsigned char dstEchoInstanceCode 106 | , std::string dstEchoAddress); 107 | virtual ~Getter(); 108 | virtual ProfileObject::Getter& reqGetProperty(unsigned char epc); 109 | 110 | virtual ProfileObject::Getter& reqGetFaultStatus(); 111 | virtual ProfileObject::Getter& reqGetManufacturerCode(); 112 | virtual ProfileObject::Getter& reqGetPlaceOfBusinessCode(); 113 | virtual ProfileObject::Getter& reqGetProductCode(); 114 | virtual ProfileObject::Getter& reqGetSerialNumber(); 115 | virtual ProfileObject::Getter& reqGetDateOfManufacture(); 116 | virtual ProfileObject::Getter& reqGetStatusChangeAnnouncementPropertyMap(); 117 | virtual ProfileObject::Getter& reqGetSetPropertyMap(); 118 | virtual ProfileObject::Getter& reqGetGetPropertyMap(); 119 | }; 120 | 121 | class Informer : public EchoObject::Informer { 122 | public: 123 | Informer(unsigned short echoClassCode, unsigned char echoInstanceCode 124 | , std::string dstEchoAddress, bool isSelfObject); 125 | virtual ~Informer(); 126 | virtual ProfileObject::Informer& reqInformProperty(unsigned char epc); 127 | 128 | virtual ProfileObject::Informer& reqInformFaultStatus(); 129 | virtual ProfileObject::Informer& reqInformManufacturerCode(); 130 | virtual ProfileObject::Informer& reqInformPlaceOfBusinessCode(); 131 | virtual ProfileObject::Informer& reqInformProductCode(); 132 | virtual ProfileObject::Informer& reqInformSerialNumber(); 133 | virtual ProfileObject::Informer& reqInformDateOfManufacture(); 134 | virtual ProfileObject::Informer& reqInformStatusChangeAnnouncementPropertyMap(); 135 | virtual ProfileObject::Informer& reqInformSetPropertyMap(); 136 | virtual ProfileObject::Informer& reqInformGetPropertyMap(); 137 | }; 138 | 139 | class InformerC : public EchoObject::InformerC { 140 | public: 141 | InformerC(unsigned short srcEchoClassCode, unsigned char srcEchoInstanceCode 142 | , std::string dstEchoAddress); 143 | virtual ~InformerC(); 144 | virtual ProfileObject::InformerC& reqInformProperty(unsigned char epc); 145 | }; 146 | }; 147 | 148 | }; 149 | 150 | #endif /* PROFILEOBJECT_H_ */ 151 | -------------------------------------------------------------------------------- /src/tutorial1/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Tutorial1.cpp 3 | * 4 | * Created on: 2014/05/02 5 | * Author: Fumiaki 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | #include "../openecho/OpenECHO.h" 12 | 13 | using namespace std; 14 | using namespace sonycsl_openecho; 15 | 16 | 17 | class DefaultNodeProfile : public NodeProfile { 18 | 19 | public: 20 | DefaultNodeProfile() : NodeProfile() {} 21 | virtual ~DefaultNodeProfile(){} 22 | protected: 23 | virtual shared_ptr > getManufacturerCode() { 24 | return shared_ptr >(); 25 | } 26 | virtual shared_ptr > getOperatingStatus() { 27 | return shared_ptr >(); 28 | } 29 | virtual shared_ptr > getIdentificationNumber() { 30 | return shared_ptr >(); 31 | } 32 | }; 33 | 34 | class DefaultController : public DeviceObject { 35 | 36 | public: 37 | DefaultController() : DeviceObject() {} 38 | virtual ~DefaultController(){} 39 | 40 | virtual unsigned short getEchoClassCode() { 41 | return 0x05FF; 42 | } 43 | protected: 44 | virtual shared_ptr > getOperationStatus() { 45 | return shared_ptr >(); 46 | } 47 | virtual bool setInstallationLocation(vector& edt) { 48 | return false; 49 | } 50 | virtual shared_ptr > getInstallationLocation() { 51 | return shared_ptr >(); 52 | } 53 | virtual shared_ptr > getFaultStatus() { 54 | return shared_ptr >(); 55 | } 56 | virtual shared_ptr > getManufacturerCode() { 57 | return shared_ptr >(); 58 | } 59 | }; 60 | 61 | 62 | int main() { 63 | shared_ptr profile(new DefaultNodeProfile()); 64 | vector > devices; 65 | devices.push_back(shared_ptr(new DefaultController())); 66 | 67 | //Echo::addEventListener(std::shared_ptr(new Echo::Logger())); 68 | Echo::start(profile, devices); 69 | 70 | while(true) { 71 | NodeProfile::Getter(NodeProfile::ECHO_CLASS_CODE 72 | , NodeProfile::INSTANCE_CODE 73 | , EchoSocket::MULTICAST_ADDRESS).reqGetSelfNodeInstanceListS().send(); 74 | 75 | 76 | vector > nodes = Echo::getNodes(); 77 | shared_ptr local = Echo::getSelfNode(); 78 | 79 | for(shared_ptr en : nodes) { 80 | if(en.get()->isSelfNode()) { 81 | cout << "Node id = " << en.get()->getAddress() << "(local)" << endl; 82 | } else { 83 | cout << "Node id = " << en.get()->getAddress() << endl; 84 | } 85 | 86 | cout << " Node Profile = instanceCode:" << hex << (int)en.get()->getNodeProfile().get()->getInstanceCode() << endl; 87 | 88 | cout << " Devices" << endl; 89 | vector > dos = en.get()->getDevices(); 90 | 91 | for(shared_ptr d : dos) { 92 | cout << " class:" << hex << d.get()->getEchoClassCode() << ",instanceCode:" << hex << (int)d.get()->getInstanceCode() << endl; 93 | } 94 | cout << "----" << endl; 95 | } 96 | cout << "--------" << endl; 97 | 98 | 99 | sleep(10); 100 | } 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /src/tutorial2a/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Tutorial1.cpp 3 | * 4 | * Created on: 2014/05/02 5 | * Author: Fumiaki 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | #include "../openecho/OpenECHO.h" 12 | 13 | using namespace std; 14 | // OpenECHOのnamespaceは"sonycsl_openecho" 15 | using namespace sonycsl_openecho; 16 | 17 | 18 | class MyControllerReceiver : public DeviceObject::Receiver { 19 | public: 20 | MyControllerReceiver(){} 21 | virtual ~MyControllerReceiver(){} 22 | protected: 23 | virtual void onGetOperationStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success) { 24 | cout << "Controller power : "; 25 | for(unsigned char b : property.edt) { 26 | cout << hex << (int)b << " "; 27 | } 28 | cout << endl; 29 | } 30 | virtual void onGetInstallationLocation(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success) { 31 | cout << "Installation location : "; 32 | for(unsigned char b : property.edt) { 33 | cout << hex << (int)b << " "; 34 | } 35 | cout << endl; 36 | } 37 | }; 38 | class MyPowerDistributionBoardMeteringReceiver : public DeviceObject::Receiver { 39 | public: 40 | MyPowerDistributionBoardMeteringReceiver(){} 41 | virtual ~MyPowerDistributionBoardMeteringReceiver(){} 42 | protected: 43 | 44 | virtual void onGetOperationStatus(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success) { 45 | cout << "PowerDistributionBoardMetering power : "; 46 | for(unsigned char b : property.edt) { 47 | cout << hex << (int)b << " "; 48 | } 49 | cout << endl; 50 | } 51 | virtual bool onGetProperty(std::shared_ptr eoj, unsigned short tid, unsigned char esv, EchoProperty& property, bool success) { 52 | 53 | bool ret = DeviceObject::Receiver::onGetProperty(eoj, tid, esv, property, success); 54 | if(ret) return true; 55 | switch(property.epc) { 56 | case ((unsigned char)0xC7) : 57 | cout << "GetMeasuredInstantaneousCurrents IntCur : "; 58 | for(unsigned char b : property.edt) { 59 | cout << hex << (int)b << " "; 60 | } 61 | cout << endl; 62 | return true; 63 | default : 64 | return false; 65 | } 66 | } 67 | 68 | }; 69 | 70 | class MyEventListener : public Echo::EventListener { 71 | virtual void onNewDeviceObject(std::shared_ptr device) { 72 | if(device.get()->getEchoClassCode() == 0x05FF) { 73 | cout << "Controller found." << endl; 74 | device.get()->setReceiver(shared_ptr(new MyControllerReceiver())); 75 | device.get()->get().reqGetOperationStatus().reqGetInstallationLocation().send(); 76 | } else if(device.get()->getEchoClassCode() == 0x0287) { 77 | cout << "PowerDistributionBoardMetering found." << endl; 78 | device.get()->setReceiver(shared_ptr(new MyPowerDistributionBoardMeteringReceiver())); 79 | device.get()->get().reqGetProperty((unsigned char)0xC7).reqGetOperationStatus().send(); 80 | } 81 | } 82 | }; 83 | 84 | class DefaultNodeProfile : public NodeProfile { 85 | 86 | public: 87 | DefaultNodeProfile() : NodeProfile() {} 88 | virtual ~DefaultNodeProfile(){} 89 | protected: 90 | virtual shared_ptr > getManufacturerCode() { 91 | return shared_ptr >(); 92 | } 93 | virtual shared_ptr > getOperatingStatus() { 94 | return shared_ptr >(); 95 | } 96 | virtual shared_ptr > getIdentificationNumber() { 97 | return shared_ptr >(); 98 | } 99 | }; 100 | 101 | class DefaultController : public DeviceObject { 102 | 103 | public: 104 | DefaultController() : DeviceObject() {} 105 | virtual ~DefaultController(){} 106 | 107 | virtual unsigned short getEchoClassCode() { 108 | return 0x05FF; 109 | } 110 | protected: 111 | virtual shared_ptr > getOperationStatus() { 112 | return shared_ptr >(); 113 | } 114 | virtual bool setInstallationLocation(vector& edt) { 115 | return false; 116 | } 117 | virtual shared_ptr > getInstallationLocation() { 118 | return shared_ptr >(); 119 | } 120 | virtual shared_ptr > getFaultStatus() { 121 | return shared_ptr >(); 122 | } 123 | virtual shared_ptr > getManufacturerCode() { 124 | return shared_ptr >(); 125 | } 126 | }; 127 | 128 | 129 | int main() { 130 | 131 | shared_ptr eventListener(new MyEventListener()); 132 | Echo::addEventListener(eventListener); 133 | 134 | shared_ptr profile(new DefaultNodeProfile()); 135 | vector > devices; 136 | devices.push_back(shared_ptr(new DefaultController())); 137 | 138 | //Echo::addEventListener(std::shared_ptr(new Echo::Logger())); 139 | 140 | Echo::start(profile, devices); 141 | 142 | while(true) { 143 | NodeProfile::Getter(NodeProfile::ECHO_CLASS_CODE 144 | , NodeProfile::INSTANCE_CODE 145 | , EchoSocket::MULTICAST_ADDRESS).reqGetSelfNodeInstanceListS().send(); 146 | 147 | 148 | sleep(10); 149 | } 150 | 151 | 152 | 153 | 154 | return 0; 155 | } 156 | -------------------------------------------------------------------------------- /src/tutorial4/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SonyCSL/OpenECHO-cpp/507d2e0e7bfd1171e19b48f77d0ad7ddcc815ff5/src/tutorial4/main -------------------------------------------------------------------------------- /src/tutorial4/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Tutorial1.cpp 3 | * 4 | * Created on: 2014/05/02 5 | * Author: Fumiaki 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | #include "../openecho/OpenECHO.h" 12 | 13 | using namespace std; 14 | // OpenECHOのnamespaceは"sonycsl_openecho" 15 | using namespace sonycsl_openecho; 16 | 17 | 18 | class DefaultNodeProfile : public NodeProfile { 19 | 20 | public: 21 | DefaultNodeProfile() : NodeProfile() {} 22 | virtual ~DefaultNodeProfile(){} 23 | protected: 24 | virtual shared_ptr > getManufacturerCode() { 25 | return shared_ptr >(); 26 | } 27 | virtual shared_ptr > getOperatingStatus() { 28 | return shared_ptr >(); 29 | } 30 | virtual shared_ptr > getIdentificationNumber() { 31 | return shared_ptr >(); 32 | } 33 | }; 34 | 35 | class LightEmulator : public DeviceObject { 36 | protected: 37 | shared_ptr > mStatus; 38 | shared_ptr > mLocation; 39 | shared_ptr > mFaultStatus; 40 | shared_ptr > mManufacturerCode; 41 | shared_ptr > mLightingMode; 42 | shared_ptr > mColor; 43 | static const unsigned char EPC_LIGHTING_MODE_SETTING = 0xB6; 44 | static const unsigned char EPC_RGB_SETTING_FOR_COLOR_LIGHTING = 0xC0; 45 | public: 46 | LightEmulator() : DeviceObject() { 47 | mStatus = shared_ptr >(new vector()); 48 | mStatus.get()->push_back(0x30); 49 | mLocation = shared_ptr >(new vector()); 50 | mLocation.get()->push_back(0x00); 51 | mFaultStatus = shared_ptr >(new vector()); 52 | mFaultStatus.get()->push_back(0x42); 53 | mManufacturerCode = shared_ptr >(new vector()); 54 | mManufacturerCode.get()->push_back(0x00); 55 | mManufacturerCode.get()->push_back(0x00); 56 | mManufacturerCode.get()->push_back(0x00); 57 | mLightingMode = shared_ptr >(new vector()); 58 | mLightingMode.get()->push_back(0x45); 59 | mColor = shared_ptr >(new vector()); 60 | mColor.get()->push_back(0xFF); 61 | mColor.get()->push_back(0xFF); 62 | mColor.get()->push_back(0xFF); 63 | } 64 | virtual ~LightEmulator(){} 65 | 66 | virtual unsigned short getEchoClassCode() { 67 | return 0x0290; 68 | } 69 | protected: 70 | 71 | virtual void setupPropertyMaps() { 72 | DeviceObject::setupPropertyMaps(); 73 | addSetProperty(EPC_OPERATION_STATUS); 74 | addSetProperty(EPC_LIGHTING_MODE_SETTING); 75 | addSetProperty(EPC_RGB_SETTING_FOR_COLOR_LIGHTING); 76 | addGetProperty(EPC_LIGHTING_MODE_SETTING); 77 | addGetProperty(EPC_RGB_SETTING_FOR_COLOR_LIGHTING); 78 | 79 | } 80 | 81 | 82 | virtual bool setProperty(EchoProperty& property) { 83 | 84 | bool success = DeviceObject::setProperty(property); 85 | if(success) return true; 86 | 87 | switch(property.epc) { 88 | case EPC_LIGHTING_MODE_SETTING : return setLightingModeSetting(property.edt); 89 | case EPC_RGB_SETTING_FOR_COLOR_LIGHTING : return setRgbSettingForColorLighting(property.edt); 90 | default : return false; 91 | } 92 | } 93 | 94 | virtual std::shared_ptr > getProperty( 95 | unsigned char epc) { 96 | 97 | std::shared_ptr > edt = DeviceObject::getProperty(epc); 98 | if(edt.get() != nullptr) return edt; 99 | 100 | switch(epc) { 101 | case EPC_LIGHTING_MODE_SETTING : return getLightingModeSetting(); 102 | case EPC_RGB_SETTING_FOR_COLOR_LIGHTING : return getRgbSettingForColorLighting(); 103 | default : return std::shared_ptr >(); 104 | } 105 | } 106 | 107 | virtual bool isValidProperty(EchoProperty& property) { 108 | 109 | bool valid = DeviceObject::isValidProperty(property); 110 | if(valid) return true; 111 | 112 | switch(property.epc) { 113 | case EPC_LIGHTING_MODE_SETTING : return isValidLightingModeSetting(property.edt); 114 | case EPC_RGB_SETTING_FOR_COLOR_LIGHTING : return isValidRgbSettingForColorLighting(property.edt); 115 | default : return false; 116 | } 117 | } 118 | 119 | 120 | bool setOperationStatus(vector& edt) { 121 | (*(mStatus.get()))[0] = edt.at(0); 122 | if(mStatus.get()->at(0) == 0x30) { 123 | cout << "Light ON" << endl; 124 | } else { 125 | cout << "Light OFF" << endl; 126 | } 127 | inform().reqInformOperationStatus().send(); 128 | return true; 129 | } 130 | virtual shared_ptr > getOperationStatus() { 131 | return mStatus; 132 | } 133 | virtual bool setInstallationLocation(vector& edt) { 134 | return false; 135 | } 136 | virtual shared_ptr > getInstallationLocation() { 137 | return mLocation; 138 | } 139 | virtual shared_ptr > getFaultStatus() { 140 | return mFaultStatus; 141 | } 142 | virtual shared_ptr > getManufacturerCode() { 143 | return mManufacturerCode; 144 | } 145 | 146 | 147 | bool setLightingModeSetting(vector& edt) { 148 | (*(mLightingMode.get()))[0] = edt.at(0); 149 | 150 | return true; 151 | } 152 | 153 | shared_ptr > getLightingModeSetting() { 154 | return mLightingMode; 155 | } 156 | 157 | bool isValidLightingModeSetting(vector& edt) { 158 | if(edt.size() != 1) {return false;} 159 | if(edt.at(0) == 0x41) {return true;} 160 | if(edt.at(0) == 0x42) {return true;} 161 | if(edt.at(0) == 0x43) {return true;} 162 | if(edt.at(0) == 0x45) {return true;} 163 | return false; 164 | } 165 | 166 | bool setRgbSettingForColorLighting(vector& edt) { 167 | (*(mColor.get()))[0] = edt.at(0); 168 | (*(mColor.get()))[1] = edt.at(1); 169 | (*(mColor.get()))[2] = edt.at(2); 170 | cout << "R:" << (int)(mColor.get()->at(0)) 171 | << ",G:" << (int)(mColor.get()->at(1)) 172 | << ",B:" << (int)(mColor.get()->at(2)) << endl; 173 | return true; 174 | } 175 | 176 | shared_ptr > getRgbSettingForColorLighting() { 177 | return mColor; 178 | } 179 | 180 | bool isValidRgbSettingForColorLighting(vector& edt) { 181 | if(edt.size() != 3) {return false;} 182 | return true; 183 | } 184 | }; 185 | 186 | 187 | int main() { 188 | 189 | shared_ptr profile(new DefaultNodeProfile()); 190 | vector > devices; 191 | devices.push_back(shared_ptr(new LightEmulator())); 192 | 193 | //Echo::addEventListener(std::shared_ptr(new Echo::Logger())); 194 | 195 | Echo::start(profile, devices); 196 | 197 | while(true) { 198 | NodeProfile::Getter(NodeProfile::ECHO_CLASS_CODE 199 | , NodeProfile::INSTANCE_CODE 200 | , EchoSocket::MULTICAST_ADDRESS).reqGetSelfNodeInstanceListS().send(); 201 | 202 | 203 | sleep(10); 204 | } 205 | 206 | 207 | 208 | 209 | return 0; 210 | } 211 | -------------------------------------------------------------------------------- /src/tutorial4/man.cpp: -------------------------------------------------------------------------------- 1 | // /* 2 | // * Tutorial1.cpp 3 | // * 4 | // * Created on: 2014/05/02 5 | // * Author: Fumiaki 6 | // */ 7 | 8 | 9 | // #include 10 | // #include 11 | // #include "../openecho/OpenECHO.h" 12 | 13 | // using namespace std; 14 | // using namespace sonycsl_openecho; 15 | 16 | 17 | // class DefaultNodeProfile : public NodeProfile { 18 | 19 | // public: 20 | // DefaultNodeProfile() : NodeProfile() {} 21 | // virtual ~DefaultNodeProfile(){} 22 | // protected: 23 | // virtual shared_ptr > getManufacturerCode() { 24 | // return shared_ptr >(); 25 | // } 26 | // virtual shared_ptr > getOperatingStatus() { 27 | // return shared_ptr >(); 28 | // } 29 | // virtual shared_ptr > getIdentificationNumber() { 30 | // return shared_ptr >(); 31 | // } 32 | // }; 33 | 34 | // // class HouseholdNode : public NodeProfile { 35 | // // protected: 36 | // // shared_ptr > mStatus; 37 | // // shared_ptr > mLocation; 38 | // // shared_ptr > mFaultStatus; 39 | // // shared_ptr > mManufacturerCode; 40 | // // static const unsigned char EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION = 0xE0; 41 | 42 | // // public: 43 | // // HouseholdNode() : NodeProfile() { 44 | // // // mStatus = shared_ptr >(new vector()); 45 | // // // mStatus.get()->push_back(0x30); 46 | // // // mLocation = shared_ptr >(new vector()); 47 | // // // mLocation.get()->push_back(0x00); 48 | // // // mFaultStatus = shared_ptr >(new vector()); 49 | // // // mFaultStatus.get()->push_back(0x42); 50 | // // // mManufacturerCode = shared_ptr >(new vector()); 51 | // // // mManufacturerCode.get()->push_back(0x00); 52 | // // // mManufacturerCode.get()->push_back(0x00); 53 | // // // mManufacturerCode.get()->push_back(0x00); 54 | // // } 55 | // // virtual ~HouseholdNode(){} 56 | 57 | // // virtual unsigned short getEchoClassCode() { 58 | // // return 0x0279; 59 | // // } 60 | // // protected: 61 | 62 | // // // virtual void setupPropertyMaps() { 63 | // // // NodeProfile::setupPropertyMaps(); 64 | // // // addGetProperty(EPC_OPERATING_STATUS); 65 | // // // addGetProperty(EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION); 66 | 67 | 68 | // // // } 69 | 70 | 71 | // // // virtual bool setProperty(EchoProperty& property) { 72 | 73 | // // // bool success = NodeProfile::setProperty(property); 74 | // // // if(success) return true; 75 | 76 | // // // switch(property.epc) { 77 | // // // default : return false; 78 | // // // } 79 | // // // } 80 | 81 | // // // virtual std::shared_ptr > getProperty( 82 | // // // unsigned char epc) { 83 | 84 | // // // std::shared_ptr > edt = NodeProfile::getProperty(epc); 85 | // // // if(edt.get() != nullptr) return edt; 86 | 87 | // // // switch(epc) { 88 | // // // case EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION: 89 | // // // return getMeasuredInstantaneousAmountOfElectricityGenerated(); 90 | // // // default : return std::shared_ptr >(); 91 | // // // } 92 | // // // } 93 | 94 | // // // virtual bool isValidProperty(EchoProperty& property) { 95 | 96 | // // // bool valid = NodeProfile::isValidProperty(property); 97 | // // // if(valid) return true; 98 | 99 | // // // switch(property.epc) { 100 | // // // case EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION: 101 | // // // return isValidMeasuredInstantaneousAmountOfElectricityGenerated(property.edt); 102 | // // // default : return false; 103 | // // // } 104 | // // // } 105 | 106 | 107 | // // // bool setOperationStatus(vector& edt) { 108 | // // // (*(mStatus.get()))[0] = edt.at(0); 109 | // // // if(mStatus.get()->at(0) == 0x30) { 110 | // // // cout << "ON" << endl; 111 | // // // } else { 112 | // // // cout << " OFF" << endl; 113 | // // // } 114 | // // // inform().reqInformOperationStatus().send(); 115 | // // // return true; 116 | // // // } 117 | // // // virtual shared_ptr > getOperationStatus() { 118 | // // // return mStatus; 119 | // // // } 120 | 121 | // // // virtual bool setInstallationLocation(vector& edt) { 122 | // // // return false; 123 | // // // } 124 | // // // virtual shared_ptr > getInstallationLocation() { 125 | // // // return mLocation; 126 | // // // } 127 | // // // virtual shared_ptr > getFaultStatus() { 128 | // // // return mFaultStatus; 129 | // // // } 130 | // // // virtual shared_ptr > getManufacturerCode() { 131 | // // // return mManufacturerCode; 132 | // // // } 133 | // // // shared_ptr > getMeasuredInstantaneousAmountOfElectricityGenerated() 134 | // // // { 135 | // // // cout << "getMeasuredInstantaneousAmountOfElectricityGenerated function call" << endl; 136 | // // // return NULL; 137 | // // // } 138 | // // // bool isValidMeasuredInstantaneousAmountOfElectricityGenerated(std::vector &edt) 139 | // // // { 140 | // // // if (edt.empty() || edt.size() != 2) 141 | // // // return false; 142 | // // // return true; 143 | // // // } 144 | // // virtual shared_ptr> getManufacturerCode() 145 | // // { 146 | // // return shared_ptr>(); 147 | // // } 148 | // // virtual shared_ptr> getOperatingStatus() 149 | // // { 150 | // // return shared_ptr>(); 151 | // // } 152 | // // virtual shared_ptr> getIdentificationNumber() 153 | // // { 154 | // // return shared_ptr>(); 155 | // // } 156 | // // }; 157 | 158 | // class Household : public DeviceObject 159 | // { 160 | // protected: 161 | // shared_ptr> mStatus; 162 | // shared_ptr> mLocation; 163 | // shared_ptr> mFaultStatus; 164 | // shared_ptr> mManufacturerCode; 165 | // static const unsigned char EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION = 0xE0; 166 | // static const unsigned char EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION = 0xE1; 167 | 168 | // public: 169 | // Household() : DeviceObject() 170 | // { 171 | // mStatus = shared_ptr>(new vector()); 172 | // mStatus.get()->push_back(0x30); 173 | // mLocation = shared_ptr>(new vector()); 174 | // mLocation.get()->push_back(0x00); 175 | // mFaultStatus = shared_ptr>(new vector()); 176 | // mFaultStatus.get()->push_back(0x42); 177 | // mManufacturerCode = shared_ptr>(new vector()); 178 | // mManufacturerCode.get()->push_back(0x00); 179 | // mManufacturerCode.get()->push_back(0x00); 180 | // mManufacturerCode.get()->push_back(0x00); 181 | // } 182 | // virtual ~Household() {} 183 | // virtual unsigned short getEchoClassCode() 184 | // { 185 | // return 0x0279; 186 | // } 187 | 188 | // protected: 189 | // virtual void setupPropertyMaps() 190 | // { 191 | // DeviceObject::setupPropertyMaps(); 192 | // addSetProperty(EPC_OPERATION_STATUS); 193 | // addGetProperty(EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION); 194 | // addGetProperty(EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION); 195 | // } 196 | // virtual bool setProperty(EchoProperty& property) { 197 | 198 | // bool success = DeviceObject::setProperty(property); 199 | // if(success) return true; 200 | 201 | // switch(property.epc) { 202 | // default : return false; 203 | // } 204 | // } 205 | // virtual std::shared_ptr> getProperty( 206 | // unsigned char epc) 207 | // { 208 | 209 | // std::shared_ptr> edt = DeviceObject::getProperty(epc); 210 | // if (edt.get() != nullptr) 211 | // return edt; 212 | 213 | // switch (epc) 214 | // { 215 | // case EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION: 216 | // return getMeasuredInstantaneousAmountOfElectricityGenerated(); 217 | // case EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION: 218 | // return getMeasuredCumulativeAmountOfElectricityGenerated(); 219 | // default: 220 | // return std::shared_ptr>(); 221 | // } 222 | // } 223 | // virtual shared_ptr > getMeasuredInstantaneousAmountOfElectricityGenerated() { 224 | // cout << "getMeasuredInstantaneousAmountOfElectricityGenerated function call" << endl; 225 | // return NULL; 226 | // } 227 | // virtual shared_ptr > getMeasuredCumulativeAmountOfElectricityGenerated() { 228 | // cout << "getMeasuredCumulativeAmountOfElectricityGenerated function call" << endl; 229 | // return NULL; 230 | 231 | // } 232 | // virtual bool isValidProperty(EchoProperty &property) 233 | // { 234 | // bool valid = DeviceObject::isValidProperty(property); 235 | // if (valid) 236 | // return true; 237 | 238 | // switch (property.epc) 239 | // { 240 | // case EPC_MEASURED_INSTANTANEOUS_ELECTRICITY_GENERATION: 241 | // return isValidMeasuredInstantaneousAmountOfElectricityGenerated(property.edt); 242 | // case EPC_MEASURED_CUMULATIVE_AMOUT_OF_ELECTRICITY_GENERATION: 243 | // return isValidMeasuredCumulativeAmountOfElectricityGenerated(property.edt); 244 | // default: 245 | // return false; 246 | // } 247 | // } 248 | // bool isValidMeasuredInstantaneousAmountOfElectricityGenerated(std::vector &edt) 249 | // { 250 | // if (edt.empty() || edt.size() != 2) 251 | // return false; 252 | // return true; 253 | // } 254 | // bool isValidMeasuredCumulativeAmountOfElectricityGenerated(std::vector &edt) 255 | // { 256 | // if (edt.empty() || edt.size() != 4) 257 | // return false; 258 | // return true; 259 | // } 260 | // bool setOperationStatus(vector &edt) 261 | // { 262 | // (*(mStatus.get()))[0] = edt.at(0); 263 | // if (mStatus.get()->at(0) == 0x30) 264 | // { 265 | // cout << "ON" << endl; 266 | // } 267 | // else 268 | // { 269 | // cout << "OFF" << endl; 270 | // } 271 | // DeviceObject::inform().reqInformOperationStatus().send(); 272 | // return true; 273 | // } 274 | // virtual shared_ptr > getOperationStatus() { 275 | // return mStatus; 276 | // } 277 | // virtual bool setInstallationLocation(vector& edt) { 278 | // return false; 279 | // } 280 | // virtual shared_ptr > getInstallationLocation() { 281 | // return mLocation; 282 | // } 283 | // virtual shared_ptr > getFaultStatus() { 284 | // return mFaultStatus; 285 | // } 286 | // virtual shared_ptr > getManufacturerCode() { 287 | // return mManufacturerCode; 288 | // } 289 | 290 | // }; 291 | 292 | 293 | // int main() { 294 | 295 | // shared_ptr profile(new DefaultNodeProfile()); 296 | // vector > devices; 297 | // devices.push_back(shared_ptr(new Household())); 298 | 299 | // //Echo::addEventListener(std::shared_ptr(new Echo::Logger())); 300 | 301 | // Echo::start(profile, devices); 302 | 303 | // while(true) { 304 | // NodeProfile::Getter(NodeProfile::ECHO_CLASS_CODE 305 | // , NodeProfile::INSTANCE_CODE 306 | // , EchoSocket::MULTICAST_ADDRESS).reqGetSelfNodeInstanceListS().send(); 307 | 308 | // vector > nodes = Echo::getNodes(); 309 | // shared_ptr local = Echo::getSelfNode(); 310 | 311 | // for(shared_ptr en : nodes) { 312 | // if(en.get()->isSelfNode()) { 313 | // cout << "Node id = " << en.get()->getAddress() << "(local)" << endl; 314 | // } else { 315 | // cout << "Node id = " << en.get()->getAddress() << endl; 316 | // } 317 | 318 | // cout << " Node Profile = instanceCode:" << hex << (int)en.get()->getNodeProfile().get()->getInstanceCode() << endl; 319 | 320 | // cout << " Devices" << endl; 321 | // vector > dos = en.get()->getDevices(); 322 | 323 | // for(shared_ptr d : dos) { 324 | // cout << " class:" << hex << d.get()->getEchoClassCode() << ",instanceCode:" << hex << (int)d.get()->getInstanceCode() << endl; 325 | // } 326 | // cout << "----" << endl; 327 | // } 328 | // cout << "--------" << endl; 329 | 330 | 331 | // sleep(10); 332 | // } 333 | 334 | 335 | 336 | 337 | // return 0; 338 | // } 339 | 340 | 341 | 342 | 343 | // // #include 344 | // // #include 345 | // // #include "../openecho/OpenECHO.h" 346 | 347 | // // using namespace std; 348 | // // // OpenECHOのnamespaceは"sonycsl_openecho" 349 | // // using namespace sonycsl_openecho; 350 | 351 | 352 | // // class DefaultNodeProfile : public NodeProfile { 353 | 354 | // // public: 355 | // // DefaultNodeProfile() : NodeProfile() {} 356 | // // virtual ~DefaultNodeProfile(){} 357 | // // protected: 358 | // // virtual shared_ptr > getManufacturerCode() { 359 | // // return shared_ptr >(); 360 | // // } 361 | // // virtual shared_ptr > getOperatingStatus() { 362 | // // return shared_ptr >(); 363 | // // } 364 | // // virtual shared_ptr > getIdentificationNumber() { 365 | // // return shared_ptr >(); 366 | // // } 367 | // // }; 368 | 369 | // // class LightEmulator : public DeviceObject { 370 | // // protected: 371 | // // shared_ptr > mStatus; 372 | // // shared_ptr > mLocation; 373 | // // shared_ptr > mFaultStatus; 374 | // // shared_ptr > mManufacturerCode; 375 | // // shared_ptr > mLightingMode; 376 | // // shared_ptr > mColor; 377 | // // static const unsigned char EPC_LIGHTING_MODE_SETTING = 0xB6; 378 | // // static const unsigned char EPC_RGB_SETTING_FOR_COLOR_LIGHTING = 0xC0; 379 | // // public: 380 | // // LightEmulator() : DeviceObject() { 381 | // // mStatus = shared_ptr >(new vector()); 382 | // // mStatus.get()->push_back(0x30); 383 | // // mLocation = shared_ptr >(new vector()); 384 | // // mLocation.get()->push_back(0x00); 385 | // // mFaultStatus = shared_ptr >(new vector()); 386 | // // mFaultStatus.get()->push_back(0x42); 387 | // // mManufacturerCode = shared_ptr >(new vector()); 388 | // // mManufacturerCode.get()->push_back(0x00); 389 | // // mManufacturerCode.get()->push_back(0x00); 390 | // // mManufacturerCode.get()->push_back(0x00); 391 | // // mLightingMode = shared_ptr >(new vector()); 392 | // // mLightingMode.get()->push_back(0x45); 393 | // // mColor = shared_ptr >(new vector()); 394 | // // mColor.get()->push_back(0xFF); 395 | // // mColor.get()->push_back(0xFF); 396 | // // mColor.get()->push_back(0xFF); 397 | // // } 398 | // // virtual ~LightEmulator(){} 399 | 400 | // // virtual unsigned short getEchoClassCode() { 401 | // // return 0x0290; 402 | // // } 403 | // // protected: 404 | 405 | // // virtual void setupPropertyMaps() { 406 | // // DeviceObject::setupPropertyMaps(); 407 | // // addSetProperty(EPC_OPERATION_STATUS); 408 | // // addSetProperty(EPC_LIGHTING_MODE_SETTING); 409 | // // addSetProperty(EPC_RGB_SETTING_FOR_COLOR_LIGHTING); 410 | // // addGetProperty(EPC_LIGHTING_MODE_SETTING); 411 | // // addGetProperty(EPC_RGB_SETTING_FOR_COLOR_LIGHTING); 412 | 413 | // // } 414 | 415 | 416 | // // virtual bool setProperty(EchoProperty& property) { 417 | 418 | // // bool success = DeviceObject::setProperty(property); 419 | // // if(success) return true; 420 | 421 | // // switch(property.epc) { 422 | // // case EPC_LIGHTING_MODE_SETTING : return setLightingModeSetting(property.edt); 423 | // // case EPC_RGB_SETTING_FOR_COLOR_LIGHTING : return setRgbSettingForColorLighting(property.edt); 424 | // // default : return false; 425 | // // } 426 | // // } 427 | 428 | // // virtual std::shared_ptr > getProperty( 429 | // // unsigned char epc) { 430 | 431 | // // std::shared_ptr > edt = DeviceObject::getProperty(epc); 432 | // // if(edt.get() != nullptr) return edt; 433 | 434 | // // switch(epc) { 435 | // // case EPC_LIGHTING_MODE_SETTING : return getLightingModeSetting(); 436 | // // case EPC_RGB_SETTING_FOR_COLOR_LIGHTING : return getRgbSettingForColorLighting(); 437 | // // default : return std::shared_ptr >(); 438 | // // } 439 | // // } 440 | 441 | // // virtual bool isValidProperty(EchoProperty& property) { 442 | 443 | // // bool valid = DeviceObject::isValidProperty(property); 444 | // // if(valid) return true; 445 | 446 | // // switch(property.epc) { 447 | // // case EPC_LIGHTING_MODE_SETTING : return isValidLightingModeSetting(property.edt); 448 | // // case EPC_RGB_SETTING_FOR_COLOR_LIGHTING : return isValidRgbSettingForColorLighting(property.edt); 449 | // // default : return false; 450 | // // } 451 | // // } 452 | 453 | 454 | // // bool setOperationStatus(vector& edt) { 455 | // // (*(mStatus.get()))[0] = edt.at(0); 456 | // // if(mStatus.get()->at(0) == 0x30) { 457 | // // cout << "Light ON" << endl; 458 | // // } else { 459 | // // cout << "Light OFF" << endl; 460 | // // } 461 | // // inform().reqInformOperationStatus().send(); 462 | // // return true; 463 | // // } 464 | // // virtual shared_ptr > getOperationStatus() { 465 | // // return mStatus; 466 | // // } 467 | // // virtual bool setInstallationLocation(vector& edt) { 468 | // // return false; 469 | // // } 470 | // // virtual shared_ptr > getInstallationLocation() { 471 | // // return mLocation; 472 | // // } 473 | // // virtual shared_ptr > getFaultStatus() { 474 | // // return mFaultStatus; 475 | // // } 476 | // // virtual shared_ptr > getManufacturerCode() { 477 | // // return mManufacturerCode; 478 | // // } 479 | 480 | 481 | // // bool setLightingModeSetting(vector& edt) { 482 | // // (*(mLightingMode.get()))[0] = edt.at(0); 483 | 484 | // // return true; 485 | // // } 486 | 487 | // // shared_ptr > getLightingModeSetting() { 488 | // // return mLightingMode; 489 | // // } 490 | 491 | // // bool isValidLightingModeSetting(vector& edt) { 492 | // // if(edt.size() != 1) {return false;} 493 | // // if(edt.at(0) == 0x41) {return true;} 494 | // // if(edt.at(0) == 0x42) {return true;} 495 | // // if(edt.at(0) == 0x43) {return true;} 496 | // // if(edt.at(0) == 0x45) {return true;} 497 | // // return false; 498 | // // } 499 | 500 | // // bool setRgbSettingForColorLighting(vector& edt) { 501 | // // (*(mColor.get()))[0] = edt.at(0); 502 | // // (*(mColor.get()))[1] = edt.at(1); 503 | // // (*(mColor.get()))[2] = edt.at(2); 504 | // // cout << "R:" << (int)(mColor.get()->at(0)) 505 | // // << ",G:" << (int)(mColor.get()->at(1)) 506 | // // << ",B:" << (int)(mColor.get()->at(2)) << endl; 507 | // // return true; 508 | // // } 509 | 510 | // // shared_ptr > getRgbSettingForColorLighting() { 511 | // // return mColor; 512 | // // } 513 | 514 | // // bool isValidRgbSettingForColorLighting(vector& edt) { 515 | // // if(edt.size() != 3) {return false;} 516 | // // return true; 517 | // // } 518 | // // }; 519 | 520 | 521 | // // int main() { 522 | 523 | // // shared_ptr profile(new DefaultNodeProfile()); 524 | // // vector > devices; 525 | // // devices.push_back(shared_ptr(new LightEmulator())); 526 | 527 | // // //Echo::addEventListener(std::shared_ptr(new Echo::Logger())); 528 | 529 | // // Echo::start(profile, devices); 530 | 531 | // // while(true) { 532 | // // NodeProfile::Getter(NodeProfile::ECHO_CLASS_CODE 533 | // // , NodeProfile::INSTANCE_CODE 534 | // // , EchoSocket::MULTICAST_ADDRESS).reqGetSelfNodeInstanceListS().send(); 535 | 536 | 537 | // // sleep(10); 538 | // // } 539 | 540 | 541 | 542 | 543 | // // return 0; 544 | // // } --------------------------------------------------------------------------------