OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <dlfcn.h> |
| 6 #include <glib-object.h> |
| 7 #include <map> |
| 8 #include <vector> |
| 9 |
| 10 #include <base/logging.h> |
| 11 #include <base/time.h> |
| 12 #include <base/values.h> |
| 13 |
| 14 #include "chromeos_cros_api.h" // NOLINT |
| 15 #include "chromeos_network.h" // NOLINT |
| 16 #include "chromeos/dbus/dbus.h" // NOLINT |
| 17 #include "chromeos/glib/object.h" // NOLINT |
| 18 #include "monitor_utils.h" //NOLINT |
| 19 |
| 20 static void PrintProperty(const char* path, |
| 21 const char* key, |
| 22 const Value* value) { |
| 23 std::string prelude("PropertyChanged ["); |
| 24 prelude += path; |
| 25 prelude += "] "; |
| 26 prelude += key; |
| 27 prelude += " : "; |
| 28 if (value->IsType(Value::TYPE_STRING)) { |
| 29 std::string strval; |
| 30 value->GetAsString(&strval); |
| 31 LOG(INFO) << prelude << "\"" << strval << "\""; |
| 32 } else if (value->IsType(Value::TYPE_BOOLEAN)) { |
| 33 bool boolval; |
| 34 value->GetAsBoolean(&boolval); |
| 35 LOG(INFO) << prelude << boolval; |
| 36 } else if (value->IsType(Value::TYPE_INTEGER)) { |
| 37 int intval; |
| 38 value->GetAsInteger(&intval); |
| 39 LOG(INFO) << prelude << intval; |
| 40 } else if (value->IsType(Value::TYPE_LIST)) { |
| 41 const ListValue* list = static_cast<const ListValue*>(value); |
| 42 Value *itemval; |
| 43 std::string liststr; |
| 44 size_t index = 0; |
| 45 while (list->Get(index, &itemval)) { |
| 46 if (!itemval->IsType(Value::TYPE_STRING)) { |
| 47 ++index; |
| 48 continue; |
| 49 } |
| 50 std::string itemstr; |
| 51 itemval->GetAsString(&itemstr); |
| 52 liststr += itemstr; |
| 53 ++index; |
| 54 if (index < list->GetSize()) |
| 55 liststr += ", "; |
| 56 } |
| 57 LOG(INFO) << prelude << "\"" << liststr << "\""; |
| 58 } else if (value->IsType(Value::TYPE_DICTIONARY)) { |
| 59 const DictionaryValue* dict = static_cast<const DictionaryValue*>(value); |
| 60 std::string items; |
| 61 std::string itemval; |
| 62 size_t n = 0; |
| 63 DictionaryValue::key_iterator iter = dict->begin_keys(); |
| 64 while (iter != dict->end_keys()) { |
| 65 std::string key = *iter; |
| 66 items += "{'" + key + "': '"; |
| 67 if (dict->GetStringWithoutPathExpansion(key, &itemval)) |
| 68 items += itemval + "'}"; |
| 69 else |
| 70 items += "<not-a-string>'}"; |
| 71 if (n < dict->size()) |
| 72 items += ", "; |
| 73 ++iter; |
| 74 ++n; |
| 75 } |
| 76 LOG(INFO) << prelude << items; |
| 77 } else |
| 78 LOG(INFO) << prelude << "<type " << value->GetType() << ">"; |
| 79 } |
| 80 |
| 81 class CallbackMonitorNetwork { |
| 82 public: |
| 83 static void Run(void *object, |
| 84 const char *path, |
| 85 const char *key, |
| 86 const Value* value) { |
| 87 PrintProperty(path, key, value); |
| 88 } |
| 89 }; |
| 90 |
| 91 // A simple program exercising the {Set,Clear}{Device,Service}Property methods. |
| 92 int main(int argc, const char** argv) { |
| 93 ::g_type_init(); |
| 94 GMainLoop* loop = ::g_main_loop_new(NULL, false); |
| 95 |
| 96 DCHECK(loop) << "Failed to create main loop"; |
| 97 if (!LoadCrosLibrary(argv)) |
| 98 LOG(INFO) << "Failed to load cros .so"; |
| 99 |
| 100 if (argc != 4) { |
| 101 LOG(INFO) << "Usage: " << argv[0] << " <path> <property> <string-value>"; |
| 102 LOG(INFO) << " " << argv[0] << " -c <path> <property>"; |
| 103 return 1; |
| 104 } |
| 105 |
| 106 bool clear; |
| 107 const char *path, *property, *value; |
| 108 if (strcmp(argv[1], "-c") == 0) { |
| 109 clear = true; |
| 110 path = argv[2]; |
| 111 property = argv[3]; |
| 112 value = NULL; |
| 113 } else { |
| 114 clear = false; |
| 115 path = argv[1]; |
| 116 property = argv[2]; |
| 117 value = argv[3]; |
| 118 } |
| 119 |
| 120 chromeos::PropertyChangeMonitor device_mon; |
| 121 if (strncmp(path, "/service/", 9) == 0) { |
| 122 LOG(INFO) << "Requesting properties messages on service '" << path << "'"; |
| 123 device_mon = chromeos::MonitorNetworkService(&CallbackMonitorNetwork::Run, |
| 124 path, NULL); |
| 125 if (clear) { |
| 126 LOG(INFO) << "Clearing property '" << property << "' on '" << path << "'"; |
| 127 chromeos::ClearNetworkServiceProperty(path, property); |
| 128 } else { |
| 129 LOG(INFO) << "Setting property '" << property << "' on '" << path << "'"; |
| 130 chromeos::SetNetworkServiceProperty(path, property, |
| 131 Value::CreateStringValue(value)); |
| 132 } |
| 133 } else if (strncmp(path, "/device/", 8) == 0) { |
| 134 LOG(INFO) << "Requesting properties messages on device '" << path << "'"; |
| 135 device_mon = chromeos::MonitorNetworkDevice(&CallbackMonitorNetwork::Run, |
| 136 path, NULL); |
| 137 if (clear) { |
| 138 LOG(INFO) << "Clearing property '" << property << "' on '" << path << "'"; |
| 139 chromeos::ClearNetworkDeviceProperty(path, property); |
| 140 } else { |
| 141 LOG(INFO) << "Setting property '" << property << "' on '" << path << "'"; |
| 142 chromeos::SetNetworkDeviceProperty(path, property, |
| 143 Value::CreateStringValue(value)); |
| 144 } |
| 145 } else { |
| 146 LOG(INFO) << "Don't know what to do with path '" << path << "' " |
| 147 << "neither a device nor a service"; |
| 148 return 1; |
| 149 } |
| 150 |
| 151 LOG(INFO) << "Starting g_main_loop."; |
| 152 |
| 153 ::g_main_loop_run(loop); |
| 154 |
| 155 LOG(INFO) << "Shutting down."; |
| 156 |
| 157 chromeos::DisconnectPropertyChangeMonitor(device_mon); |
| 158 return 0; |
| 159 } |
OLD | NEW |